1

I have an ASP .NET GridView with Paging. One column in it has a CheckBox. In a certain scenario, I want to uncheck the checkboxes that are checked.

foreach (GridViewRow dr in gvMyGridView.Rows)
{
    if (dr.RowType == DataControlRowType.DataRow)
    {
        if ((CheckBox)dr.FindControl("chkIsApplicable") != null)
        {
            ((CheckBox)dr.FindControl("chkIsApplicable")).Checked = false;
        }
    }
}  

But unfortunately because of Paging only the records that are currently shown in the Grid can be accessed in this way. I want it to apply to ALL the items in the GridView. This should happen client side and when the user commits will get saved to the database. Any way to handle this? :)

naveen
  • 53,448
  • 46
  • 161
  • 251
Ranhiru Jude Cooray
  • 19,542
  • 20
  • 83
  • 128
  • Handle that server side or client side finally? – Jahan Zinedine Dec 30 '10 at 12:00
  • I guess you can not check those checkboxes since they don't really exist till you change the page index. My suggestion would be to keep a boolean value and set it to true when user clicks on select all, when update button is clicked, you can check for this value and do your operation – Pabuc Dec 30 '10 at 12:01
  • @Jani: No change is done server side until the user selects the "Commit" button. Once it is pressed, the information is passed and saved/updated in the DB. – Ranhiru Jude Cooray Dec 30 '10 at 12:02

1 Answers1

2

Maintaining the State of Checkbox while Paging in Gridview

The Logic

Save the checked rows primary keys to a list at PageIndexChanging event.
After setting the grid to new PageIndex and re-binding the grid, populate the new page with values in the list that is mapped to the rows in grid(if any)
So you will have a collection of checked rows in a list.
Delete the list to clear all.

This is one way of doing it.
PS: Its a two year old post, so you can surely optimise it with C# 4.0

naveen
  • 53,448
  • 46
  • 161
  • 251