session problem with textbox text

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

asp.net, visual studio 2003, IE6
I have a sample web page that is linked to another page. On the first page,
I have a text box, on the second, the first page's text box text is displayed
using a session variable. This works fine. However, when I return to the
first page, the text box content is blank.
If, prior to going to the second page, I hit 'refresh', the text disappears.
If I add a button control to the page, and click it before refreshing the
page, the text will reappear in the textbox.
If I then go to the second page and then back to the first, the textbox is
again blank.
I have added another text box that displays an incrementing counter (ie:
each time the button is clicked, the number displayed goes up by one). This
displays exactly the same behaviour as the other text box, except it will
show the next number after displaying a blank on return to the page (after
clicking the button again).

The session variable seems to be working ok, so I suppose the problem lies
with how I am trying to get it to display the textbox text.

Here's the code (currently included in the button_click event, but works the
same in page_load):
If Not Page.IsPostBack Then
Session("textinput") = TextBox1.Text
TextBox1.Text = Session("textinput")
End If

any thoughts?
 
I think its a logic error

Session("textinput") = TextBox1.Text
TextBox1.Text = Session("textinput")

Consider the case where the first page is reloaded after coming back from
the second page. Since it initially holds a String.Empty or "" upon first
initilization, your first line of code, overwrites the value in the Session
with "" (string.empty).

Then the second line tries to retrive the value back from the Session, but
the session now contains the updated value of "", so your textbox remains
empty!!

You should check the value of the TextBox before saving it to the Session
and only if its not "", then save it to session.


Regards,
Saurabh Nandu
[ www.AksTech.com ]
[ www.MasterCSharp.com ]
 
Yes, thank you. I changed it to:

If Not TextBox1.Text = String.Empty Then
Session.Contents("textinput") = TextBox1.Text
End If

and also put :

If Not Page.IsPostBack Then
TextBox1.Text = Session.Contents("textinput")
End If

into the page_load event (from the button_click where it was previously).
It now works fine.

Regards

David
 
Back
Top