Skip to main content

Simultaneous selection(deselection) all checkboxes in a GridView's column


14 Mar 2008 If you are using checkboxes in GridView in order to select a few rows, then, no doubt, you have been faced with the task how to select/deselect all checkboxes in a selected column at a time. Usually it is solved by addition of special buttons somewhere or addition a checkbox in the GridView's column header.
I want to dwell on the second variant. In order to refrain from repetition of code all of required functionality can be hidden inside of a special control. This control is inherited from the CheckBox and have only one method overriden (OnLoad). Inside the method a script is registered, this script allows to select/deselect all checkboxes in a GridView's column if a checkbox in the GridView's header is clicked. Besides, the script tracks situation when all checkboxes in rows are selected one by one and selects the checkbox in the column's header.
public class GridViewCheckBox : CheckBox
{
  protected override void OnLoad(EventArgs e)
  {
    GridViewRow parentRow = this.NamingContainer as GridViewRow;

    //checks whether the checkbox is inside of a template column (TemplateField)
    if (parentRow != null)
    {
        //registers the script
        string script = @"function gvCheckBoxClicked(sender,isHeader) {
 var cell = sender.parentNode;
 var rows = cell.parentNode.parentNode.rows;
 if (isHeader) {
     for(i=1; i<rows.length; i++)
        rows[i].cells[cell.cellIndex].getElementsByTagName(""input"")[0].checked = sender.checked;
 }
 else {
    var headerCheckBox = rows[0].cells[cell.cellIndex].getElementsByTagName(""input"")[0];
    if (!sender.checked)
         headerCheckBox.checked = false;
    else {   
          for(i=1; i<rows.length; i++) {
               if (!rows[i].cells[cell.cellIndex].getElementsByTagName(""input"")[0].checked) {
                    headerCheckBox.checked = false;
                    return;
              }
     }
     headerCheckBox.checked = true;
           }
    }
}";

        if (!Page.ClientScript.IsClientScriptBlockRegistered(typeof(Page), "GridViewCheckBox"))
            Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "GridViewCheckBox", script, true);

        //adds onclick event handler
        if (parentRow.RowType == DataControlRowType.Header)
            this.Attributes["onclick"] = "gvCheckBoxClicked(this,true);";
        else if (parentRow.RowType == DataControlRowType.DataRow)
            this.Attributes["onclick"] = "gvCheckBoxClicked(this,false);";
    }

    base.OnLoad(e);
 }
}
That is all. In order to use just add the control to HeaderTemplate and ItemTemplate of the required column:
<asp:GridView ID="GridView1" runat="server" >
   <Columns>
        <asp:TemplateField>
            <HeaderTemplate>
                <wc:GridViewCheckBox id="GridViewCheckBox1" runat="server"/>
            </HeaderTemplate>
            <ItemTemplate>
                <wc:GridViewCheckBox id="GridViewCheckBox2" runat="server"/>
            </ItemTemplate>
       </asp:TemplateField>
       <asp:BoundField ...
       ...
   </Columns>
</asp:GridView>
This is an illustration (clickable).
Author Name
Stephen Walther ASP.NET 3.5 Unleashed
Marco Bellinaso ASP.NET 2.0 Website Programming: Problem - Design - Solution (Programmer to Programmer)
Omar AL Zabir Building a Web 2.0 Portal with ASP.NET 3.5
Dino Esposito Microsoft ASP.NET 3.5 Developer Reference
Matthew MacDonald, Mario Szpuszta Pro ASP.NET 3.5 in C# 2008, Second Edition

Comments

Popular posts from this blog

Editing Child GridView in Nested GridView

Editing Child GridView in Nested GridView In this article we will explore how to edit child gridview in the nested gridview.   Let''s write some code. Step 1:  Add scriptmanager in the aspx page. < asp : ScriptManager   ID ="ScriptManager1"   runat ="server"   EnablePageMethods ="true"> </ asp : ScriptManager > Step 2:  Add below stylesheet for modal popup. < style   type ="text/css">        .modalBackground        {              background-color : Gray;              filter : alpha(opacity=80);              opacity : 0.5;       }        .ModalWindow        {              border : solid1px#c0c0c0;              background : #f0f0f0;              padding : 0px10px10px10px;              position : absolute;              top : -1000px;       } </ style > Step 3:   Create an aspx page and add a Gridview with another gridview in the last TemplateField. The last templatefield will also contain a lable which will

Scrollable Gridview With fixheader using JQuery in Asp.net

Scrollable Gridview With fixheader using JQuery in Asp.net Introduction: In this article I will explain how to implement scrollable gridview with fixed header in asp.net using JQuery.  Description:  In Previous posts I explained lot of articles regarding Gridview. Now I will explain how to implement scrollable gridview with fixed header in asp.net. I have one gridview that contains lot of records and I used  paging for gridview  but the requirement is to display all the records without paging. I removed paging at that time gridview occupied lot of space because it contains more records to solve this problem we implemented scrollbar.  After scrollbar implementation if we scroll the gridview we are unable to see Gridview header.   To implement Scrollable gridview with fixed header I tried to implement concept with css and JavaScript but there is no luck because maintaining fixed header working in IE but not in Mozilla and vice versa to solve this browser compatibility proble

Nested GridView Example In Asp.Net With Expand Collapse

This example shows how to create Nested GridView In Asp.Net Using C# And VB.NET With Expand Collapse Functionality. I have used JavaScript to Create Expandable Collapsible Effect by displaying Plus Minus image buttons. Customers and Orders Table of Northwind Database are used to populate nested GridViews. Drag and place SqlDataSource from toolbox on aspx page and configure and choose it as datasource from smart tags Go to HTML source of page and add 2 TemplateField in <Columns>, one as first column and one as last column of gridview. Place another grid in last templateField column. Markup of page after adding both templatefields will like as shown below. HTML SOURCE 1: < asp:GridView ID ="gvMaster" runat ="server" 2: AllowPaging ="True" 3: AutoGenerateColumns ="False" 4: DataKeyNames ="CustomerID" 5: DataSour