{
if (!IsPostBack)
{
//----------------------------------------------------------
// CREATE TABLE
//----------------------------------------------------------
DataTable dt = new DataTable();
dt.Columns.Add("A", System.Type.GetType("System.String"));
dt.Columns.Add("B", System.Type.GetType("System.String"));
dt.Columns.Add("C", System.Type.GetType("System.String"));
//----------------------------------------------------------
// SET DATA
//----------------------------------------------------------
for (int i = 0; i < 100; i++)
{
DataRow dr = dt.NewRow();
dr["A"] = "A" + i.ToString();
dr["B"] = "B" + i.ToString();
dr["C"] = "C" + i.ToString();
dt.Rows.Add(dr);
}
this.GridView1.DataSource = dt.DefaultView;
this.GridView1.DataBind();
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
HtmlInputCheckBox cbox = (HtmlInputCheckBox)GridView1.Rows[i].FindControl("CheckBox1");
cbox.Checked = false;
}
}
}
{
string strHiddenField1 = HiddenField1.Value;
string[] arrHiddenField1 = strHiddenField1.Split(',');
List<string> listAdd = new List<string>();
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
HtmlInputCheckBox cbox = (HtmlInputCheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (cbox.Checked == false)
{
for (int j = 0; j < arrHiddenField1.Length; j++)
{
if (String.IsNullOrEmpty(arrHiddenField1[j]))
{
continue;
}
if (GridView1.Rows[i].Cells[1].Text.Equals(arrHiddenField1[j]))
{
arrHiddenField1[j] = "";
}
}
}
else
{
bool bolExist = false;
for (int j = 0; j < arrHiddenField1.Length; j++)
{
if (String.IsNullOrEmpty(arrHiddenField1[j]))
{
continue;
}
if (GridView1.Rows[i].Cells[1].Text.Equals(arrHiddenField1[j]))
{
bolExist = true;
break;
}
}
if (!bolExist)
{
listAdd.Add(GridView1.Rows[i].Cells[1].Text);
}
}
}
strHiddenField1 = "";
for (int i = 0; i < arrHiddenField1.Length; i++)
{
if (!String.IsNullOrEmpty(arrHiddenField1[i]))
{
strHiddenField1 += arrHiddenField1[i] + ",";
}
}
for (int i = 0; i < listAdd.Count; i++)
{
if (!String.IsNullOrEmpty(listAdd[i]))
{
strHiddenField1 += listAdd[i] + ",";
}
}
HiddenField1.Value = strHiddenField1;
//----------------------------------------------------------
// CREATE TABLE
//----------------------------------------------------------
DataTable dt = new DataTable();
dt.Columns.Add("A", System.Type.GetType("System.String"));
dt.Columns.Add("B", System.Type.GetType("System.String"));
dt.Columns.Add("C", System.Type.GetType("System.String"));
//----------------------------------------------------------
// SET DATA
//----------------------------------------------------------
for (int i = 0; i < 100; i++)
{
DataRow dr = dt.NewRow();
dr["A"] = "A" + i.ToString();
dr["B"] = "B" + i.ToString();
dr["C"] = "C" + i.ToString();
dt.Rows.Add(dr);
}
this.GridView1.DataSource = dt.DefaultView;
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
arrHiddenField1 = strHiddenField1.Split(',');
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
HtmlInputCheckBox cbox = (HtmlInputCheckBox)GridView1.Rows[i].FindControl("CheckBox1");
bool bolCheck = false;
for (int j = 0; j < arrHiddenField1.Length; j++)
{
if (arrHiddenField1[j].Equals(GridView1.Rows[i].Cells[1].Text))
{
bolCheck = true;
break;
}
}
if (bolCheck)
{
cbox.Checked = true;
}
}
}
<form id="form1" runat="server">
<asp:HiddenField ID="HiddenField1" runat="server" />
<div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
OnPageIndexChanging="GridView1_PageIndexChanging">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<input id="CheckBox1" type="checkbox" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="A" HeaderText="A" />
<asp:BoundField DataField="B" HeaderText="B" />
<asp:BoundField DataField="C" HeaderText="C" />
</Columns>
</asp:GridView>
</div>
</form>
</body>