title: persistence of checkbox values in Gridview on paging asp.net/c#
body: <p>I want to persist the check box values on selection of items displayed in grid view. </p>
  
  <p>This is the code in gridviewpage.aspx which creates the gridview:</p>
  
  <pre><code> &lt;form id="form1" runat="server"&gt;
      &lt;div&gt;
         &lt;asp:GridView ID="GridView1" runat="server" 
          AutoGenerateColumns = "False" Font-Names = "Arial" 
          Font-Size = "11pt" AlternatingRowStyle-BackColor = "#C2D69B"  
          HeaderStyle-BackColor = "green" AllowPaging ="True"   
          OnPageIndexChanging = "OnPaging1xxx" OnRowDataBound = "RowDataBound" 
              AllowSorting="True" DataKeyNames="CustomerID" DataSourceID="SqlDataSource1" &gt;
         &lt;Columns&gt;
          &lt;asp:BoundField ItemStyle-Width = "150px" DataField = "CustomerID" 
                 HeaderText = "CustomerID" ReadOnly="True" SortExpression="CustomerID" /&gt;
          &lt;asp:BoundField ItemStyle-Width = "150px" DataField = "City" HeaderText = "City" 
                 SortExpression="City"/&gt;
          &lt;asp:BoundField ItemStyle-Width = "150px" DataField = "Country" 
                 HeaderText = "Country" SortExpression="Country"/&gt;
          &lt;asp:BoundField ItemStyle-Width = "150px" DataField = "PostalCode" 
                 HeaderText = "PostalCode" SortExpression="PostalCode"/&gt;
                 &lt;asp:TemplateField&gt;
                  &lt;ItemTemplate&gt;
                  &lt;asp:CheckBox id="CheckBox1" runat="server" onclick="Check_Click(this)"/&gt;
                  &lt;/ItemTemplate&gt;
                  &lt;HeaderTemplate&gt;
                  &lt;asp:CheckBox id="chkAll" runat="server" Text="Select All" onclick="checkAll(this)"/&gt;
                  &lt;/HeaderTemplate&gt;
                 &lt;/asp:TemplateField&gt;
         &lt;/Columns&gt; 
         &lt;AlternatingRowStyle BackColor="#C2D69B"  /&gt;
  
  &lt;HeaderStyle BackColor="Green"&gt;&lt;/HeaderStyle&gt;
      &lt;/asp:GridView&gt; 
          &lt;asp:SqlDataSource ID="SqlDataSource1" runat="server" 
              ConnectionString="Data Source=SDANTURTHI-PC;Initial Catalog=ArticleDB;Integrated Security=True" 
              ProviderName="System.Data.SqlClient" 
              SelectCommand="SELECT [CustomerID], [City], [Country], [PostalCode] FROM [Customers]"&gt;
          &lt;/asp:SqlDataSource&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p>This is the code in gridviewpage.aspx.cs</p>
  
  <pre><code>using System;
  using System.Data;
  using System.Configuration;
  using System.Web;
  using System.Web.Security;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Web.UI.WebControls.WebParts;
  using System.Web.UI.HtmlControls;
  using System.Data.SqlClient;
  using System.Collections;
  using System.IO.Compression;  
  
  public partial class _Default : System.Web.UI.Page 
  {
      protected void Page_Load(object sender, EventArgs e)
      {        
          ArrayList CheckBoxArray;
          if (ViewState["CheckBoxArray"] != null)
          {
              CheckBoxArray = (ArrayList)ViewState["CheckBoxArray"];
          }
          else
          {
              CheckBoxArray = new ArrayList();
          }
  
          if (IsPostBack)
          {
              int CheckBoxIndex;
              bool CheckAllWasChecked=false;
              CheckBox chkAll = (CheckBox)GridView1.HeaderRow.Cells[4].FindControl("chkAll");
              string checkAllIndex = "chkAll-" + GridView1.PageIndex;
              if (chkAll.Checked)
              {                
                  if (CheckBoxArray.IndexOf(checkAllIndex) == -1)
                  {
                      CheckBoxArray.Add(checkAllIndex);
                  }
              }
              else 
              {
                  if (CheckBoxArray.IndexOf(checkAllIndex) != -1)
                  {
                      CheckBoxArray.Remove(checkAllIndex);
                      CheckAllWasChecked = true;
                  }
              }
              for (int i = 0; i &lt; GridView1.Rows.Count; i++)
              {
                  if (GridView1.Rows[i].RowType == DataControlRowType.DataRow)
                  {
                      CheckBox chk = (CheckBox)GridView1.Rows[i].Cells[4].FindControl("CheckBox1");
                      CheckBoxIndex = GridView1.PageSize * GridView1.PageIndex + (i + 1);
                      if (chk.Checked)
                      {
                          if (CheckBoxArray.IndexOf(CheckBoxIndex) == -1 &amp;&amp; !CheckAllWasChecked)
                          {
                              CheckBoxArray.Add(CheckBoxIndex);
                          }
                      }
                      else 
                      {
                          if (CheckBoxArray.IndexOf(CheckBoxIndex) != -1 || CheckAllWasChecked)
                          {
                              CheckBoxArray.Remove(CheckBoxIndex);
                          }
                      }
                  }
              }
          }
         ViewState["CheckBoxArray"] = CheckBoxArray;
  
         GridView1.DataBind();
      }  
  
      protected void OnPaging1xxx(object sender, GridViewPageEventArgs e)
      {
  
          GridView1.PageIndex = e.NewPageIndex;
          GridView1.DataBind();
          if (ViewState["CheckBoxArray"] != null)
          {
              ArrayList CheckBoxArray = (ArrayList)ViewState["CheckBoxArray"];
              string checkAllIndex = "chkAll-" + GridView1.PageIndex;
  
              if (CheckBoxArray.IndexOf(checkAllIndex) != -1)
              {
                  CheckBox chkAll = (CheckBox)GridView1.HeaderRow.Cells[4].FindControl("chkAll");
                  chkAll.Checked = true;
              }
              for (int i = 0; i &lt; GridView1.Rows.Count; i++)
              {
  
                  if (GridView1.Rows[i].RowType == DataControlRowType.DataRow)
                  {
                      if (CheckBoxArray.IndexOf(checkAllIndex) != -1)
                      {
                          CheckBox chk = (CheckBox)GridView1.Rows[i].Cells[4].FindControl("CheckBox1");
                          chk.Checked = true;
                          GridView1.Rows[i].Attributes.Add("style","background-color:aqua");      
                      }
                      else
                      {
                          int CheckBoxIndex = GridView1.PageSize * (GridView1.PageIndex) + (i + 1);
                          if (CheckBoxArray.IndexOf(CheckBoxIndex) != -1)
                          {
                              CheckBox chk = (CheckBox)GridView1.Rows[i].Cells[4].FindControl("CheckBox1");
                              chk.Checked = true;
                              GridView1.Rows[i].Attributes.Add("style", "background-color:aqua");
                          }
                      }
                  }
              }
          }
      }
      protected void RowDataBound(object sender, GridViewRowEventArgs e)
      {
          if (e.Row.RowType == DataControlRowType.DataRow)
          {
              e.Row.Attributes.Add("onmouseover", "MouseEvents(this, event)");
              e.Row.Attributes.Add("onmouseout", "MouseEvents(this, event)");
          }
      }
  }
  </code></pre>
  
  <p>It is not able to persist the values, but when i debug the checkboxarray and <code>viewstate["checkboxarray"]</code> seem to possessing the right values. I don't understand why it is not working. Please help me. Thank you in anticipation</p>