Click here to Skip to main content
14,574,789 members

Check/uncheck CheckBox in a GridView using Javascript

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


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.


(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;

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();

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

After clicking the Select All CheckBox


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


About the Author

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

QuestionApplicable only in the First Column Pin
boybudz3213-Feb-20 21:49
Memberboybudz3213-Feb-20 21:49 
PraiseObrigado Pin
aspaiva16-Mar-18 11:23
professionalaspaiva16-Mar-18 11:23 
QuestionBut what about a nested gridview? Pin
Fandango6823-Sep-15 19:28
MemberFandango6823-Sep-15 19:28 
I don't want to toggle the checkboxes in a nested gridview, just the checkboxes from the main gridview. How is this possible please?
GeneralMy vote of 5 Pin
nuaaydh27-May-15 22:26
Membernuaaydh27-May-15 22:26 
Questionnested grid view Pin
Member 1021767823-Apr-15 21:00
MemberMember 1021767823-Apr-15 21:00 
Generalvery useful thanks Pin
mongoose_za1-Oct-14 0:19
Membermongoose_za1-Oct-14 0:19 
QuestionMy vote of 5 Pin
Rahim Lotfi13-Jul-14 11:27
MemberRahim Lotfi13-Jul-14 11:27 
GeneralMy vote of 5 Pin
Abhijoy_D6-Mar-13 20:15
MemberAbhijoy_D6-Mar-13 20:15 
GeneralMy vote of 5 Pin
vinyasAm24-Sep-12 1:27
MembervinyasAm24-Sep-12 1:27 
Answereasy way to check/uncheck Checkboxes in gridview Pin
shilpis12-Jul-12 3:02
Membershilpis12-Jul-12 3:02 
GeneralMy vote of 5 Pin
Mico Perez29-Apr-12 21:03
MemberMico Perez29-Apr-12 21:03 
GeneralMy vote of 5 Pin
Deepanjandeys5-Jan-12 2:28
MemberDeepanjandeys5-Jan-12 2:28 
GeneralMy vote of 5 Pin
Nigam Patel3-Jan-12 23:55
MemberNigam Patel3-Jan-12 23:55 
GeneralMy vote of 5 Pin
Maverick200921-Jun-11 1:29
professionalMaverick200921-Jun-11 1:29 
GeneralMy vote of 5 Pin
Habib_78619-Jun-11 20:48
MemberHabib_78619-Jun-11 20:48 
GeneralMy vote of 5 Pin
RakhySunny31-May-11 23:07
MemberRakhySunny31-May-11 23:07 
GeneralMy vote of 5 Pin
Thrinath reddy24-Feb-11 23:53
MemberThrinath reddy24-Feb-11 23:53 
GeneralMy vote of 4 Pin
Shreekanth Gaanji19-Feb-11 20:32
MemberShreekanth Gaanji19-Feb-11 20:32 
GeneralMy vote of 5 Pin
brijesh vaidya21-Dec-10 18:41
Memberbrijesh vaidya21-Dec-10 18:41 
GeneralMy vote of 1 Pin
sourabh patni27-Sep-10 0:20
Membersourabh patni27-Sep-10 0:20 
GeneralMy vote of 5 Pin
AMIT_BHAGAT12-Aug-10 22:03
MemberAMIT_BHAGAT12-Aug-10 22:03 
GeneralI made an improvement Pin
Member 6988515-Feb-10 9:50
MemberMember 6988515-Feb-10 9:50 
GeneralGridview Validation help is needed Pin
Rameez Raja24-Nov-09 18:55
MemberRameez Raja24-Nov-09 18:55 
Generalwell done Pin
Donsw18-Apr-09 10:37
MemberDonsw18-Apr-09 10:37 
Generalcheckbox not getting selected Pin
svknair27-Jan-09 19:49
Membersvknair27-Jan-09 19:49 

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

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

Posted 29 Mar 2008


57 bookmarked