C# .NET - handle session event for a text box - Asked By ram chander on 22-Jun-15 07:41 AM

I have a text box where i am assigning a value using session on page load on save am saving it which is working fine, now i want to save a new value which is entered in the textbox, on save button click the textbox is retaining the session object value on textchanged event.
Robbe Morris replied to ram chander on 22-Jun-15 07:42 AM
Are you checking the IsPostBack property in the Page_Load event to make sure you are not always reloading the textbox value?
ram chander replied to Robbe Morris on 22-Jun-15 08:12 AM
Hi,

yes am checking it
here the what am doing

protected void Page_Load(object sender, EventArgs e)
{
   if(IsPostBack)
    {
    set session ();
    }
}

private void text1_TextChanged(object sender, EventArgs e)
{
   Session.Remove("Name");
   Session["Name"]=text1.Text;
}

and the old value is retaining.

Thanks,
 
Robbe Morris replied to ram chander on 22-Jun-15 09:03 AM
IsPostBack will be true when the textchanged event fires.  I would think you'd only want to call setsession when it is false.

Also, there is no need to remove the old variable "Name" prior to giving it a new value.

 Session.Remove("Name"); // remove this line

All of that said, you might be better off setting your session values when the user clicks the save/submit button instead of relying on the event order of textbox change events.
  
ram chander replied to Robbe Morris on 22-Jun-15 09:09 AM
Ok, but the reason why i have given it in  IsPostBack () is am filling the textbox values on page load from another page
to pre populate the textboxes.
I believe if i set the session value in (!IsPostBack) textbox will not be prepopulating with session values.

Thanks
Robbe Morris replied to ram chander on 22-Jun-15 09:41 AM
That is only true if you are posting page A to page B.  If you are just redirecting from page A to page B, that isn't true.

The !IsPostBack would be the initial page load to set your textbox and ignore setting it again when the page is posted back on the textchanged event.