Question

When you need to remember state across postbacks or callbacks i am led to believe that Session shouldn't be used.

One reason for this is this will cause issues if multiple tabs are open as an action in one tab will result in the session variable being changed so it will effect the other tabs.

For example if i need to persist a collection of objects on a page i can do so via session like so.

protected void Page_Load(object sender, EventArgs e)
{
   List<MyClass> myClassList = Session["MyClassList"] as List<MyClass>;

   if(myClassLIst == null)
       myClassList = new List<MyClass>


    //Do something with collection
}

protected void AddButton_Click(object sender, EventArgs e)
{
  List<MyClass> myClassList = (List<MyClass>)Session["MyClassList"]

  myClassList.Add(new MyClass{ Name = NameTextBox.Text});

  Session["MyClassList"] = myClassList;
}

However due to the reason stated above i don't see any reason why i wouldn't just handle this using hiddenfields (Viewstate is another option) like below.

protected void Page_Load(object sender, EventArgs e)
{
   //HiddenField.Value will store json data as a string

   List<MyClass> myClassList = JsonConvert.DeserializeObject
   <List<MyClass>>(HiddenField.Value);

   //Do something with collection
}

protected void AddButton_Click(object sender, EventArgs e)
{
  List<MyClass> myClassList = JsonConvert.DeserializeObject
  <List<MyClass>>(HiddenField.Value);

  myClassList.Add(new MyClass{ Name = NameTextBox.Text});

  HiddenField.Value = JsonConvert.SerializeObject(myClassList));
}

Here i am storing the collection in a hidden field as Json and serializing/deserializing it using Json.net. This solves the issues with tabs as each page is now responsible for it's on state.

My question is am i right in concluding that Session should only be used when remembering data across multiple pages? And is using hidden fields always better? I see many example where people use session to remember state in a single page so i may be missing something.

Was it helpful?

Solution

Pretty much, yes, page specific data should not be stored in Session, since that can cause issues if multiple tabs/windows are open.

Using ViewState directly is a good alternative. Or using HiddenFields will work fine if you need to access the values in them from the client side. That's really the decision point between using ViewState and HiddenFields.

OTHER TIPS

If data is specific to a single page, use ViewState. Move to a HiddenField if that data needs to be shared with client-side code. If the data needs to persist across multiple page requests, move to Session.

A potential issue around storing data in Session is that it may be be shared across multiple browser windows. If your user opens a new window you may have conflicts between windows sharing the same session. That is fine and maybe even preferable if the data is being used to maintain a set of privileges but if the data contains a selection of a particular page, that will break your application.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top