How can I make a check all in a gridView with more than one page?

Jonathan 66 Reputation points
2022-01-26T03:24:08.48+00:00

Hello, I'm having problems doing a checkAll function in a gridView because each page of it has 50 rows, but sometimes it loads more than that, creating more than one page. I already have a check all that mark all other check's, but just on one page, if I have more than one page, the check all don't work on the second page, what can I do?

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,417 questions
0 comments No comments
{count} votes

Accepted answer
  1. Lan Huang-MSFT 28,841 Reputation points Microsoft Vendor
    2022-01-26T08:20:53.93+00:00

    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;  
                        }  
                    }  
                }  
    

    168600-1.gif

    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.


0 additional answers

Sort by: Most helpful