Not able to get all paging checkbox checked records in gridview asp.net c#

Gani_tpt 1,686 Reputation points
2024-05-13T15:30:31.1333333+00:00

I am using gridview with paging. i done all the pagination code and selected checkbox.

Checkbox "CheclALL" is working fine with every page.

But, when i save the record, it will be saving only last page checkbox checked values.

What is the problem in the code..?

i am referring the below URL for pagination and i have attached my code here.

Referring URL ==> https://www.aspsnippets.com/Articles/21/Preserving-state-of-Checkboxes-while-paging-in-ASP.Net-GridView-Control/

ASPX Page ==>

protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {

                foreach (GridViewRow gvrow in gvCustomer.Rows)
                {
                    if (gvrow.RowType == DataControlRowType.DataRow)
                    {
                        var checkbox = gvrow.FindControl("CheckBox1") as CheckBox;
                         /*******
Getting only last page record*********/
                          if (checkbox.Checked) ==>                          {                             var Remarks = (gvrow.FindControl("txtReasons") as TextBox).Text;                         }                     }                 }             }             catch (Exception ex)             {                              }             finally             {                              }       }

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,303 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,358 questions
{count} votes

Accepted answer
  1. Lan Huang-MSFT 26,121 Reputation points Microsoft Vendor
    2024-05-14T05:15:26.4966667+00:00

    Hi @Gani_tpt,

    The reason for this problem is that only the current page is fetched when pagination is enabled.

    You can use the command below to browse all pages and browse all rows in each page. You can also get the current page before doing this and return there after looping through everything.

    Below is an example I wrote based on the link you provided.

    protected void btnSave_Click(object sender, EventArgs e)
            {
                int a = gvCustomer.PageIndex;
                //Loop through All Pages
                for (int i = 0; i < gvCustomer.PageCount; i++)
                {
                    //Set Page Index
                    gvCustomer.SetPageIndex(i);
                    //After Setting Page Index Loop through its Rows
                    foreach (GridViewRow gvrow in gvCustomer.Rows)
                    {
                        if (gvrow.RowType == DataControlRowType.DataRow)
                        {
                            var checkbox = gvrow.Cells[0].FindControl("CheckBox1") as CheckBox;
                            /*******
    Getting only last page record*********/
                            if (checkbox.Checked)
                            {
                                //var Remarks = (gvrow.FindControl("txtReasons") as TextBox).Text; 
                                string CustomerID = gvrow.Cells[1].Text;
                                string City = gvrow.Cells[1].Text;
                                string Country = gvrow.Cells[3].Text;
                                string PostalCode = gvrow.Cells[4].Text;
                                //var Remarks = (gvrow.Cells[1].FindControl("txtReasons") as TextBox).Text;
                                using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConString"].ConnectionString))
                                {
                                    using (SqlCommand cmd = new SqlCommand("INSERT INTO Customers(CustomerID,City,Country,PostalCode) VALUES(@CustomerID,@City,@Country,@PostalCode)", con))
                                    {
                                        cmd.Parameters.AddWithValue("@CustomerID", CustomerID);
                                        cmd.Parameters.AddWithValue("@City", City);
                                        cmd.Parameters.AddWithValue("@Country", Country);
                                        cmd.Parameters.AddWithValue("@PostalCode", PostalCode);
                                        con.Open();
                                        cmd.ExecuteNonQuery();
                                        con.Close();
                                    }
                                }
                            }                       
                        }
                    }
                    gvCustomer.SetPageIndex(a);
                }
            }
    

    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