Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Session Variables Lag one post behind 1

Status
Not open for further replies.

wbochar

IS-IT--Management
Mar 14, 2003
72
US
I've probably implemented this wrong or have something in the wrong order. When I use code below, the session variable doesn't get updated properly.

When the code first runs there is no session, when you select a language from the drop down the code fires to update the session variable and things post back to the page.

So when you start, the language in the dropdown is english and the debug label is updated to say there is no session. Select french, it autoposts then redraws the page. The changelanguage() code alters the label currentlang with the value from the dropdown ("FR") but when the page reloads the session is still empty. If you then select Espanol in the dropdown the page reloads the page the currentlang label has "ES" and session variable now has something in it -- but its "FR". if you keep select another langauge it seems the session variable is always one step behind.

Heres the code:

Code:
<%@ Page language="c#" debug=true %>
<html>
  <head>
    <title>Session Screwy</title>
<script runat=server language="c#">

void Page_load(Object sender, EventArgs e)
{

if (Session["Language"]!=null)
	{
	DebugDump.Text="Session Not Empty: "+Session["Language"].ToString();
	}
else
	{
	DebugDump.Text="Session Empty";
	}
}

void ChangeLanguage (Object sender, EventArgs e)
{
Session["Language"]=dropLanguage.SelectedItem.Value.ToString();
CurrentLang.Text=Session["Language"].ToString();
}
 
</script>
 
</head>
<body>
	
<form id="Form1" method="post" runat="server">

<asp:Label id="CurrentLang" runat="server"></asp:Label>

<asp:DropDownList AutoPostBack=True OnSelectedIndexChanged="ChangeLanguage" id="dropLanguage" Runat="server" Font-Size="7" Height="14">
<ASP:LISTITEM Value="EN" text="English" />
<ASP:LISTITEM Value="FR" text="Français" />
<ASP:LISTITEM Value="ES" text="Espanol" />
</asp:Dropdownlist></li>
</P>
<asp:Label id="DebugDump" runat="server"></asp:Label>
</form>
</body>
</html>

-wbochar

 
Page_Load fires first, before your ChangeLanguage.
Code:
<%@ Page language="c#" debug=true %>
<HTML>
<HEAD>
<title>Session Screwy</title>
<script runat="server" language="c#">

private void Page_load(Object sender, EventArgs e)
{

}

public void ChangeLanguage (Object sender, EventArgs e)
{
	Session["Language"]=dropLanguage.SelectedItem.Value.ToString();
	CurrentLang.Text=Session["Language"].ToString();
	DisplaySession();
}

public void DisplaySession()
{
	if (Session["Language"]!=null)
	{
		DebugDump.Text="Session Not Empty: " + Session["Language"].ToString();
	}
	else
	{
		DebugDump.Text="Session Empty";
	}
}	

</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
	<asp:Label id="CurrentLang" runat="server"></asp:Label>
	<asp:DropDownList AutoPostBack="True" OnSelectedIndexChanged="ChangeLanguage" id="dropLanguage" Runat="server"
		Font-Size="7" Height="14">
		<ASP:LISTITEM Value="EN" text="English" />
		<ASP:LISTITEM Value="FR" text="Français" />
		<ASP:LISTITEM Value="ES" text="Espanol" />
	</asp:DropDownList>
	<P></P>
	<asp:Label id="DebugDump" runat="server"></asp:Label>
</form>
</body>
</HTML>
hth,
Marty
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top