Checking all CheckBoxes in a GridView

Scott Mitchell continues to post great ASP.NET content (if you haven't subscribed to his blog then you should!).

He has posted a nice new article detailing how to implement checkbox functionality within the ASP.NET 2.0 GridView, as well as add the common "Check All / Uncheck All" functionality to it.  You can read the article here.

Hope this helps,

Scott 

 

9 Comments

  • Excellent article. One thing it doesn't handle though is that if you individually check all the row checkboxes, the 'check all' checkbox remains unchecked, and conversly if you check the 'check all' checkbox then uncheck one of the rows, the 'check all' checkbox remains checked. Obviously that's not a deal breaker, but clients have complained of much more trivial things before :) The way I solved this problem was to create a custom checkbox control that let you assign a MasterCheckBoxID to it. At prerender time the checkbox would search for its master (which also had to be this custom checkbox type) and register itself with it. The master then declared a client-side array of all its slaves similar to this article, but each slave also notified the master when there was a check/uncheck. The master then searched all of its slaves and made sure its state was correct. The result is if you check all the checkboxes the master is automatically selected too, and vise versa. Since its a control its reusable in other situations not just a grid view. It also opens up the possibility of extending it so support multiple masters (think complex scenerios like KBB's vehicle feature list). One challenge with it was that in a gridview or datagrid, or repeater for that matter, each checkbox lives in a seperate naming container, which makes refering to the master ID trickier. So you could also assign the master checkbox control programmatically instead of by ID, which helped. Then I also created a custom column type (for datagrids) that did the dirty work for you.

  • Thanks for the heads-up Scott, this was an excellent article.



    Wondering if you have any info you can share with us on the status of the 38-part ASP.NET data tutorial series you hinted at back in March. Is this one still coming about?



    Thanks -- Marc

  • Hi Marc,

    The big tutorial series is on its way. We have the first 20 tutorials from Scott Mitchell and are working on the edit/publish pass now (sometimes this takes awhile with MSDN). I'm pushing hard to get them up as soon as possible.

    Thanks,

    Scott

  • Thanks for providing this link Scott!

    I have made small modification in the code to place CheckBox at the header of GridView for toggling rows CheckBoxes. Here is the URL:
    http://www.firoz.name/2006/05/30/checking-all-checkboxes-in-a-gridview/

  • thx Scott to give us such excellent articles. i still follow and check the news here everyday,feel so novelty. these days i wonder look up sth articles about linq . hope your suggestion. thx your help. special thx every authorship,UghUghUgh! Regards Yichamps yichamps@126.com

  • Great tutorial. Now that I can check or uncheck all/multiple items, what is the best strategy to process these multiple transactions at the server? Let's say 2 of 5 checkboxes where checked while databinding. After the user gets to interact with it, neither of the 2 originally checked boxes is checked but a new one is. Does anyone have any strategies for how to reconcile these changes and persist them to a database?

  • C# version for those who need it. Just place this in your page load, and use Scott's java in the head section. This is for the client side script version            CheckBox cbHeader = ((CheckBox)gvBF.HeaderRow.FindControl("HeaderLevelCheckBox"));            cbHeader.Attributes.Add("onclick", "ChangeAllCheckBoxStates(this.checked);");            ClientScript.RegisterArrayDeclaration("CheckBoxIDs", string.Concat("'", cbHeader.ClientID, "'"));            foreach (GridViewRow grv in gvBF.Rows)            {                CheckBox cb = ((CheckBox)grv.FindControl("RowLevelCheckBox"));                cb.Attributes.Add("onclick", "ChangeHeaderAsNeeded();");                ClientScript.RegisterArrayDeclaration("CheckBoxIDs", string.Concat("'", cb.ClientID, "'"));

  • For the C# implementation, I am getting the following error: System.NullReferenceException: Object reference not set to an instance of an object. CheckBox cbHeader = (CheckBox)gvSummaryGeneralMargin.HeaderRow.FindControl("CheckAll"); I can't tell what the error is...does anyone know? Thanks!

  • Article is worth while
    But the problem is that it wont work when we place two or more gridviews with differnt headers and child checkboxes. In my scenario
    i wrote:
    CheckBox cbHeader1 = ((CheckBox)(grdEvents.HeaderRow.FindControl("chkAllEvents")));
           cbHeader1.Attributes.Add("onclick", "SelectAllCheckboxes(this.checked)");
           ClientScript.RegisterArrayDeclaration("CheckBoxIDs", string.Concat("'", cbHeader1.ClientID, "'"));
           foreach (GridViewRow row in grdEvents.Rows)
           {
               CheckBox cb1 = ((CheckBox)(row.FindControl("chkEvent")));
               cb1.Attributes.Add("onclick", "BLOCKED SCRIPT return ChangeHeaderAsNeeded()");
               ClientScript.RegisterArrayDeclaration("CheckBoxIDs", string.Concat("'", cb1.ClientID, "'"));
           }
           //check box map with Gridview: exceptions
           CheckBox cbHeader2 = ((CheckBox)(grdExceptions.HeaderRow.FindControl("chkAllExceptions")));
           cbHeader2.Attributes.Add("onclick", "SelectAllCheckboxes(this.checked)");
           ClientScript.RegisterArrayDeclaration("CheckBoxIDs", string.Concat("'", cbHeader2.ClientID, "'"));
           foreach (GridViewRow row in grdExceptions.Rows)
           {
               CheckBox cb2 = ((CheckBox)(row.FindControl("chkException")));
               cb2.Attributes.Add("onclick", "BLOCKED SCRIPT return ChangeHeaderAsNeeded()");
               ClientScript.RegisterArrayDeclaration("CheckBoxIDs", string.Concat("'", cb2.ClientID, "'"));
           }
    but unfortunately first is working at each time , second one not.

Comments have been disabled for this content.