Hi @Jonathan ,
Did you set the AutoPostBack property to true.
You can refer to the example below:
.aspx
168640-test.txt
code behind
ArrayList CheckBoxArray = new ArrayList();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.GetData();
}
}
private void GetData()
{
using (SqlConnection con = new SqlConnection(@"****"))
{
using (SqlCommand cmd = new SqlCommand("SELECT CustomerID,CompanyName,ContactName,ContactTitle,City,Country FROM Customers", con))
{
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
}
}
private void PopulateGridByIndex(int index)
{
this.gvCustomers.PageIndex = index;
this.GetData();
CheckData();
}
protected void check_CheckedChanged(object sender, EventArgs e)
{
int tempIndex = -1;
CheckBox headerCheckbox = (sender as CheckBox);
bool checkedvalue = headerCheckbox.Checked;
ViewState["Uncheck"] = checkedvalue;
tempIndex = gvCustomers.PageIndex;
int pageindex = tempIndex;
for (int i = 0; i <= gvCustomers.PageCount; i++)
{
if (i == gvCustomers.PageCount)
{
PopulateGridByIndex(0);
}
else
{
PopulateGridByIndex(i);
}
string checkAllIndex = "chkAll-" + gvCustomers.PageIndex;
if (checkedvalue)
{
CheckBoxArray.Add(checkAllIndex);
}
foreach (GridViewRow row in gvCustomers.Rows)
{
(row.FindControl("checkbox") as CheckBox).Checked = checkedvalue;
}
((CheckBox)gvCustomers.HeaderRow.Cells[0].FindControl("checkAll")).Checked = checkedvalue;
}
ViewState["CheckBoxArray"] = CheckBoxArray;
}
protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
PopulateGridByIndex(e.NewPageIndex);
CheckData();
}
private void CheckData()
{
if (ViewState["CheckBoxArray"] != null)
{
ArrayList CheckBoxArray = (ArrayList)ViewState["CheckBoxArray"];
string checkAllIndex = "chkAll-" + gvCustomers.PageIndex;
if (CheckBoxArray.IndexOf(checkAllIndex) != -1)
{
foreach (GridViewRow row in gvCustomers.Rows)
{
(row.FindControl("checkbox") as CheckBox).Checked = true;
}
CheckBox chkAll = (CheckBox)gvCustomers.HeaderRow.Cells[0].FindControl("checkAll");
chkAll.Checked = true;
}
}
}
Best regards,
Lan Huang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.