Click here to Skip to main content
11,412,834 members (75,268 online)
Click here to Skip to main content

Check/uncheck CheckBox in a GridView using Javascript

, 29 Mar 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
Check/uncheck CheckBox control inside a GridView using javascript without postback.

Introduction

Check/uncheck CheckBox control inside a GridView using javascript without postback. This article will give an idea on how to achieve this task.

Some sample code is provided under Using the code heading and complete code is available in the project download.

Background

(Forums are flooded with the questions about how to check/uncheck CheckBox control inside a GridView using javascript without postback.

How To’s:

In this article a GridView control is used. Inside the GridView control there are 3 fields. One template field and two are bound fields to display some sample data.

Inside the TemplateField a CheckBox control is placed in the HeaderTemplate (with ID cbSelectAll and Text Select All), ItemTemplate and AlternatingItemTemplate.

Using the code

Code that will be required to achieve this task.

Javascript that will be required to perform the Select All action is as follow:

    <script type="text/javascript">
        function <code>SelectAll(id)</code>
        {
            //get reference of GridView control
            <code>var</code> grid = document.getElementById("<%= GridView1.ClientID %>");
            //variable to contain the cell of the grid
            <code>var</code> 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>		

Code required to add an attribute of cbSelectAll CheckBox is as follow:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //Binding GridView with the datasource

            //FillDataTable is a method that will return a DataTable
            //filled with some rows (code available in download)
            this.GridView1.DataSource = FillDataTable();
            this.GridView1.DataBind();
        }
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        { 
            //Find the checkbox control in header and add an attribute
            ((CheckBox)e.Row.FindControl("cbSelectAll")).Attributes.Add("onclick", "javascript:SelectAll('" + 
                    ((CheckBox)e.Row.FindControl("cbSelectAll")).ClientID + "')");
        }
    }

Before clicking the Select All CheckBox
GridCheckBox

After clicking the Select All CheckBox
GridCheckBox

License

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

Share

About the Author

farazsk11
Web Developer
United Arab Emirates United Arab Emirates
Faraz is working as a Senior Software Engineer for a company located in Sharjah, UAE. He likes developing new applications with the latest technologies. Mostly reponsible for web applications using Microsoft.Net. He has done MCPD so far. Other than work play guitars, sing and play PSP.

Comments and Discussions

 
Questionnested grid view Pin
Member 10217678 at 23-Apr-15 22:00
memberMember 1021767823-Apr-15 22:00 
Generalvery useful thanks Pin
mongoose_za at 1-Oct-14 1:19
membermongoose_za1-Oct-14 1:19 
QuestionMy vote of 5 Pin
Rahim Lotfi at 13-Jul-14 12:27
memberRahim Lotfi13-Jul-14 12:27 
GeneralMy vote of 5 Pin
Abhijoy_D at 6-Mar-13 21:15
memberAbhijoy_D6-Mar-13 21:15 
GeneralMy vote of 5 Pin
vinyasAm at 24-Sep-12 2:27
membervinyasAm24-Sep-12 2:27 
Answereasy way to check/uncheck Checkboxes in gridview Pin
shilpis at 12-Jul-12 4:02
membershilpis12-Jul-12 4:02 
GeneralMy vote of 5 Pin
Mico Perez at 29-Apr-12 22:03
memberMico Perez29-Apr-12 22:03 
GeneralMy vote of 5 Pin
Deepanjan Dey at 5-Jan-12 3:28
memberDeepanjan Dey5-Jan-12 3:28 
GeneralMy vote of 5 Pin
Nigam Patel at 4-Jan-12 0:55
memberNigam Patel4-Jan-12 0:55 
GeneralMy vote of 5 Pin
Maverick2009 at 21-Jun-11 2:29
memberMaverick200921-Jun-11 2:29 
Excellent illustration of how to manage checkboxes in a gridview
GeneralMy vote of 5 Pin
Habib2010 at 19-Jun-11 21:48
memberHabib201019-Jun-11 21:48 
GeneralMy vote of 5 Pin
Member 4313471 at 1-Jun-11 0:07
memberMember 43134711-Jun-11 0:07 
GeneralMy vote of 5 Pin
Thrinath reddy at 25-Feb-11 0:53
memberThrinath reddy25-Feb-11 0:53 
GeneralMy vote of 4 Pin
shreekanth Gaanji at 19-Feb-11 21:32
membershreekanth Gaanji19-Feb-11 21:32 
GeneralMy vote of 5 Pin
brijesh vaidya at 21-Dec-10 19:41
memberbrijesh vaidya21-Dec-10 19:41 
GeneralMy vote of 1 Pin
sourabh patni at 27-Sep-10 1:20
membersourabh patni27-Sep-10 1:20 
GeneralMy vote of 5 Pin
AMIT_BHAGAT at 12-Aug-10 23:03
memberAMIT_BHAGAT12-Aug-10 23:03 
GeneralI made an improvement Pin
Member 698851 at 5-Feb-10 10:50
memberMember 6988515-Feb-10 10:50 
GeneralGridview Validation help is needed Pin
Rameez Raja at 24-Nov-09 19:55
memberRameez Raja24-Nov-09 19:55 
Generalwell done Pin
Donsw at 18-Apr-09 11:37
memberDonsw18-Apr-09 11:37 
Generalcheckbox not getting selected Pin
svknair at 27-Jan-09 20:49
membersvknair27-Jan-09 20:49 
GeneralRe: checkbox not getting selected Pin
farazsk11 at 28-Jan-09 0:33
memberfarazsk1128-Jan-09 0:33 
GeneralGreat Snippet Pin
Mico_Perez_II at 12-Jan-09 22:36
memberMico_Perez_II12-Jan-09 22:36 
GeneralRe: Great Snippet Pin
farazsk11 at 13-Jan-09 0:12
memberfarazsk1113-Jan-09 0:12 
GeneralRe: Great Snippet Pin
Mico_Perez_II at 13-Jan-09 0:51
memberMico_Perez_II13-Jan-09 0:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150427.1 | Last Updated 29 Mar 2008
Article Copyright 2008 by farazsk11
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid