views:

162

answers:

3

I set a session object at one juncture in my code:

Session("my_name") = "Dave"

Later in my code I give the user a chance to update this object:

Session("my_name") = TextBox1.Text

I reload my page and display a little hello statement like this:

Label1.Text = "Hello" & CStr(Session("my_name"))

The result is: "Hello Dave" no matter what I change Session("my_name") too.

EDIT: Here is the a full code-behind I wrote up to demonstrated:

Public Class WebForm1
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Response.Cache.SetCacheability(HttpCacheability.NoCache)
    Response.ExpiresAbsolute = DateTime.Now.AddMonths(-1)
    If Page.IsPostBack = False Then
        Session("my_name") = "Dave"
    End If
    Label1.Text = CStr(Session("my_name"))
End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    Session("my_name") = TextBox1.Text
End Sub

End Class

+4  A: 

The Page's Load event fires up sooner than the Button's click event. Therefore, at the time it runs, the value of Session("my_name") is still "Dave".

If you'd like to set it up correctly, you should either put the Label1.Text = CStr(Session("my_name")) into the PreRender event handler of your page.

You cut put it into the Button's Click event as well (after setting the session value, of course), but I guess that you want to use the session later for storing objects for less trivial purposes.

(I guess that you'd like to use the session for more advanced purposes later. After all, what would be the point of using session if you only want to change a label's text?)

Basically, here is what you want:

  • The Page_Load sets Session("my_name") to "Dave" if it is not a postback
  • The Button1_Click sets Session("my_name") to the textbox's text
  • The Page_PreRender sets the label's text.

Here's what's happening with your current code:

  • The Page_Load sets Session("my_name") to "Dave" if it is not a postback
  • The Page_Load sets the label's text
  • The Button1_Click sets Session("my_name") to the textbox's text

You can read more about the topic in here: ASP.NET Page Life Cycle Overview.

Venemo
Venemo - Thanks for the answer. I think you are right about the Page Load firing earlier than the button click event...but I don't understand exactly what you mean when you say, "or into the button's Click event" in the latter part - isn't that what I am already doing and what you just said isn't occurring until after page_load?
davemackey
No, I mean that you should set the Label's text in the Button's Click event. You are setting it in the Page_Load currently. But if you are using session for a less trivial case, I'd certainly recommend the PreRender event of the page. (I edited my post to clarify.)
Venemo
Thanks for your help. This issue has plagued me...It appears to be cleared up now.
davemackey
+2  A: 

The Page.Load runs before your Button1_Click - so you are setting the value of your textbox to whatever you have in session, and then a bit later taking the contents of that textbox (which you have already overwritten) and popping it back into session.

slugster
Ugg, too slow with my answer, well answered by @Venemo :)
slugster
+2  A: 

You're not setting your Session variable default properly. Basically you're setting the session variable to 'Dave' on every page load that's not a postback. That even includes callbacks and page reloads.

To set the Session default try...

if( String.IsNullOrEmpty(Session["my_name"]) )
{
    Session["my_name"] = "Dave";
}

Now you can use your session variable without testing if it's part of a Postback or Callback.

kervin