Click here to Skip to main content
15,881,092 members
Articles / Programming Languages / Java

Select all rows from one checkbox in a GridView

Rate me:
Please Sign up or sign in to vote.
4.85/5 (4 votes)
11 Oct 2013CPOL 40.4K   3  
Today i'm going to show you how to select all records in a gridview from one checkbox which located in gridview header.First set gridview like below

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

Today i'm going to show you how to select all records in a gridview from one checkbox which located in gridview header.

First set gridview like below code snnippet

<asp:GridView ID="GridView1" runat="server" 
AutoGenerateColumns="False" 
            
onrowdatabound="GridView1_RowDataBound" 
            
            >
            <Columns>
                <asp:TemplateField>
                
                <HeaderTemplate>
                <asp:CheckBox ID="allchk"  
                    runat="server" Text="All" />
                </HeaderTemplate>
                <ItemTemplate>
                
                <asp:CheckBox ID="selectchk" 
                    runat="server" />
               
                </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="TR. ID">
                
                <ItemTemplate>
                <asp:Label ID="namelbl" 
                    runat="server" Text='<%#Eval(
                    "name") %>'></asp:Label>
                </ItemTemplate>
                 </asp:TemplateField>
                
            </Columns>
        </asp:GridView>

In the next step you have to write small java script code.

<script type="text/javascript">
        function SelectAll(id)
        {
            //get reference of GridView control
            var grid = document.getElementById("<%= GridView1.ClientID %>");
            //variable to contain the cell of the grid
            var cell;
            
            if (grid.rows.length > 0)
            {
                //loop starts from 1. rows[0] points to the header.
                for (i=1; i<grid.rows.length; i++)
                {
                    //get the reference of first column
                    cell = grid.rows[i].cells[0];
                    
                    //loop according to the number of childNodes in the cell
                    for (j=0; j<cell.childNodes.length; j++)
                    {           
                        //if childNode type is CheckBox                 
                        if (cell.childNodes[j].type =="checkbox")
                        {
                        //assign the status of the Select All checkbox to the cell 
                        //checkbox within the grid
                            cell.childNodes[j].checked = document.getElementById(id).checked;
                        }
                    }
                }
            }
        }
    </script>

Next, in gridview rowdatabound event add javascript function to the all select checkbox onclick event.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)<br />
       {

          //header select all function
           if (e.Row.RowType == DataControlRowType.Header)
           {
               ((CheckBox)e.Row.FindControl("allchk")).Attributes.Add("onclick",
                   "javascript:SelectAll('" +
                   ((CheckBox)e.Row.FindControl("allchk")).ClientID + "')");
           }

           }

Happy programming!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

755 members

Comments and Discussions

 
-- There are no messages in this forum --