title: Is using hidden fields always better than session when remembering state on a single page?
body: <p>When you need to remember state across postbacks or callbacks i am led to believe that Session shouldn't be used. </p>
  
  <p>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. </p>
  
  <p>For example if i need to persist a collection of objects on a page i can do so via session like so.</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
     List&lt;MyClass&gt; myClassList = Session["MyClassList"] as List&lt;MyClass&gt;;
  
     if(myClassLIst == null)
         myClassList = new List&lt;MyClass&gt;
  
  
      //Do something with collection
  }
  
  protected void AddButton_Click(object sender, EventArgs e)
  {
    List&lt;MyClass&gt; myClassList = (List&lt;MyClass&gt;)Session["MyClassList"]
  
    myClassList.Add(new MyClass{ Name = NameTextBox.Text});
  
    Session["MyClassList"] = myClassList;
  }
  </code></pre>
  
  <p>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.</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
     //HiddenField.Value will store json data as a string
  
     List&lt;MyClass&gt; myClassList = JsonConvert.DeserializeObject
     &lt;List&lt;MyClass&gt;&gt;(HiddenField.Value);
  
     //Do something with collection
  }
  
  protected void AddButton_Click(object sender, EventArgs e)
  {
    List&lt;MyClass&gt; myClassList = JsonConvert.DeserializeObject
    &lt;List&lt;MyClass&gt;&gt;(HiddenField.Value);
  
    myClassList.Add(new MyClass{ Name = NameTextBox.Text});
  
    HiddenField.Value = JsonConvert.SerializeObject(myClassList));
  }
  </code></pre>
  
  <p>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. </p>
  
  <p>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.</p>