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.
So, I was testing this, and I forgot to say, I have a little problem, when I click to change the page, it does a postback and the checks id's on the second name, receive the same id as those on the first one, I think this is because the data is in a session. So I don't think I can use the name as you did here " string checkAllIndex = "chkAll-" + gvCustomers.PageIndex;". Is there a way to not do a PostBack?168825-froncode.txt168813-codebehind.txt
If you want to see a part of the code to understand better, here it is
Hi @Jonathan ,
This code basically uses an ArrayList to store the checked checkbox,
then checks if the ArrayList is stored in ViewState, if so, gets it from ViewState,
and if the checkbox is checked, adds a reference to the ArrayList in chkAll-0 format, where 0 is the page index of the GridView page.
You say you don't get this line of code, is there a specific error message?
Best regards,
Lan Huang
Hello @Lan Huang-MSFT , sorry for the delay, i didn't receive any notification that you had reply my question.
No, i didn't get any error message, but I was not understanding what that line of code was doing, thanks, the only problem that I am having is the Grid doing a postback when I change the page. Do you know if this is good to do ? Or is it better changing the page without doing a postback?
Hi @Jonathan ,
If you don't want to postback, you can try to wrap the content in UpdatePanel to achieve partial refresh,
you can refer to the example provided in my answer.
Best regards,
Lan Huang
That's the problem, it's already inside in an Update Panel
Hi @Jonathan ,
Maybe you try adding a postback trigger for the grid in the update panel.
https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.postbacktrigger?view=netframework-4.8
and in Page_Load you can encapsulate your code like this:
Best regards,
Lan Huang
Thanks man, i'll teste here later
Sign in to comment
0 additional answers
Sort by: Most helpful