title: ASP.NET Session State & Postback problems
body: <p>I have a problem that I believe is a session state issue, but I'm at a loss to figure out what's wrong.  I have a sample project to illustrate the problem.  (Code below)  I have 2 buttons.  Each populates a List with some unique data and then uses that data to add a row to a table.  The row contains text boxes so that the user can edit the data.  (For my sample, there's no update button to persist the data.)  To reproduce the problem in VS2010, create a new "ASP.NET Web Application" project and copy/paste the aspx code and the c# code-behind into Default.aspx, then run the application.</p>
  
  <ul>
  <li>Press the DataSet 1 button and the grid should populate with 1 row.</li>
  <li>Edit the data in one of the text boxes and tab off of the text box.  (The newly entered text should remian, and the font should be blue.  This is what I want to happen.)</li>
  <li>Now click either of the DataSet buttons to reset the List and refresh the table.
  Edit the data in one of the text boxes and tab off the text box.  (Immediately, the text in the box refreshes back to its original value.  This only happens once, though.  If you edit either text box now, it will work normally.)</li>
  <li>This is repeatable... the first edit after pressing the DataSet buttons a 2nd, 3rd, etc. time gets reset back to the original value.  And I can't figure out why.</li>
  </ul>
  
  <hr>
  
  <pre><code>&lt;%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
      CodeBehind="Default.aspx.cs" Inherits="DebugPostbackIssue._Default" %&gt;
  
  &lt;asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"&gt;
  &lt;/asp:Content&gt;
  &lt;asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"&gt;
      &lt;h2&gt;
          Welcome to ASP.NET!
      &lt;/h2&gt;
      &lt;p&gt;
          Populate the table with DataSet #1:&lt;asp:Button runat="server" ID="btnDS1" Text="Dataset 1" OnClick="btnDS1_Click" /&gt;
      &lt;/p&gt;
      &lt;p&gt;
          Populate the table with DataSet #2:&lt;asp:Button runat="server" ID="btnDS2" Text="Dataset 2" OnClick="btnDS2_Click" /&gt;
      &lt;/p&gt;
      &lt;p&gt;
          &lt;asp:Table runat="server" ID="tblData"&gt;
              &lt;asp:TableHeaderRow runat="server" ID="thrData"&gt;
                  &lt;asp:TableHeaderCell Scope="Column" Text="Column 1"&gt;&lt;/asp:TableHeaderCell&gt;
                  &lt;asp:TableHeaderCell Scope="Column" Text="Column 2"&gt;&lt;/asp:TableHeaderCell&gt;
              &lt;/asp:TableHeaderRow&gt;
          &lt;/asp:Table&gt;
      &lt;/p&gt;
  &lt;/asp:Content&gt;
  </code></pre>
  
  <hr>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  
  namespace DebugPostbackIssue
  {
      public partial class _Default : System.Web.UI.Page
      {
          private List&lt;string&gt; _MyData = new List&lt;string&gt;();
  
          protected void Page_Load(object sender, EventArgs e)
          {
  
          }
  
          protected void Page_Init(object sender, EventArgs e)
          {
              LoadSessionData();
              GenerateGrid(false);
          }
  
          protected void btnDS1_Click(object sender, EventArgs e)
          {
              _MyData = new List&lt;string&gt;();
              _MyData.Add("111");
              _MyData.Add("aaa");
  
              SaveSessionData();
              GenerateGrid(true);
          }
  
          protected void btnDS2_Click(object sender, EventArgs e)
          {
              _MyData = new List&lt;string&gt;();
              _MyData.Add("222");
              _MyData.Add("bbb");
  
              SaveSessionData();
              GenerateGrid(true);
          }
  
          private void SaveSessionData()
          {
              Session["MyData"] = _MyData;
          }
  
          private void LoadSessionData()
          {
              if (Session["MyData"] != null)
                  _MyData = (List&lt;string&gt;)Session["MyData"];
              else
                  _MyData = new List&lt;string&gt;();
          }
  
          private void GenerateGrid(bool ClearData)
          {
              if (ClearData)
                  while (tblData.Rows.Count &gt; 1)
                      tblData.Rows.Remove(tblData.Rows[tblData.Rows.Count - 1]);
  
              TableRow tr = new TableRow();
  
              foreach (string s in _MyData)
              {
                  TableCell tc = new TableCell();
  
                  TextBox txtBox = new TextBox();
                  txtBox.Text = s;
                  txtBox.Attributes.Add("OriginalValue", s);
                  txtBox.TextChanged += new EventHandler(txtBox_TextChanged);
                  txtBox.AutoPostBack = true;
                  tc.Controls.Add(txtBox);
  
                  tr.Cells.Add(tc);
              }
  
              if (tr.Cells.Count &gt; 0)
                  tblData.Rows.Add(tr);
          }
  
          void txtBox_TextChanged(object sender, EventArgs e)
          {
              TextBox Sender = (TextBox)sender;
              if (Sender.Text == Sender.Attributes["OriginalValue"])
                  Sender.ForeColor = System.Drawing.Color.Black;
              else
                  Sender.ForeColor = System.Drawing.Color.Blue;
          }
      }
  }
  </code></pre>