title: Variables lost on postback in asp.net
body: <p>I have a gridview with some data and I want to add a checkbox column which can choose multiple rows. By clicking on it I want to save an primary key of row and change css class of row.</p>
  
  <p>Using <a href="http://www.asp.net/web-forms/tutorials/data-access/enhancing-the-gridview/adding-a-gridview-column-of-checkboxes-cs" rel="nofollow">this article(step 2)</a> I created itemtemplate,added there a checkbox(specifying ID as TransactionSelector), and add a checkedChange() to it. There I only change a css class of row and add a row index to arraylist. But when I click button with event which show this list, it has no items. </p>
  
  <pre><code>        &lt;asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" &gt;
          &lt;Columns&gt;
              &lt;asp:TemplateField&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:CheckBox ID="TransactionSelector" runat="server" 
                          oncheckedchanged="TransactionSelector_CheckedChanged" AutoPostBack="True" /&gt;
                  &lt;/ItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
              &lt;asp:BoundField DataField="iTransactionsId" HeaderText="iTransactionsId" 
                  SortExpression="iTransactionsId" /&gt;
              &lt;asp:BoundField DataField="mAmount" HeaderText="mAmount" 
                  SortExpression="mAmount" /&gt;
              &lt;asp:BoundField DataField="vchTransactionType" HeaderText="vchTransactionType" 
                  SortExpression="vchTransactionType" /&gt;
              &lt;asp:BoundField DataField="dtDate" HeaderText="dtDate" 
                  SortExpression="dtDate" /&gt;
              &lt;asp:BoundField DataField="cStatus" HeaderText="cStatus" 
                  SortExpression="cStatus" /&gt;
              &lt;asp:BoundField DataField="test123" HeaderText="test123" 
                  SortExpression="test123" /&gt;
          &lt;/Columns&gt;
          &lt;RowStyle CssClass="unselectedRow" /&gt;
      &lt;/asp:GridView&gt;
  
  &lt;/asp:Panel&gt;
  
  &lt;asp:Panel ID="InfoPanel" runat="server" CssClass="infoPanel"&gt;
  
      &lt;asp:Button ID="ShowSelected" runat="server" Text="Button" 
          onclick="ShowSelected_Click" /&gt;
  
      &lt;asp:Label ID="InfoLabel" runat="server"&gt;&lt;/asp:Label&gt;
  
  &lt;/asp:Panel&gt;
  </code></pre>
  
  <p>C Sharp code:</p>
  
  <pre><code>public partial class WebForm1 : System.Web.UI.Page
  {
      ArrayList indices = new ArrayList();
  
  
  protected void Page_Load(object sender, EventArgs e)
  {
     if (!IsPostBack)
       {
         GridView1.DataSourceID = "SqlDataSource1";
         GridView1.DataBind();
       }
  }
  
  
  
  protected void TransactionSelector_CheckedChanged(object sender, EventArgs e)
  {
     CheckBox cb = (CheckBox)sender;
     GridViewRow row = (GridViewRow)cb.NamingContainer;
     // row.CssClass = (cb.Checked) ? "selectedRow" : "unselectedRow";
     if (cb.Checked)
     {
       row.CssClass = "selectedRow";
       indices.Add(row.RowIndex);
     }
     else
     {
       row.CssClass = "unselectedRow";
       indices.Remove(row.RowIndex);
     }
  }
  
  protected void ShowSelected_Click(object sender, EventArgs e)
  {
    InfoLabel.Text = "";
    foreach (int i in indices)
    {
     InfoLabel.Text += i.ToString() + "&lt;br&gt;";
    }
  }
  
  }
  }
  </code></pre>