5,660,782 members and growing! (15,649 online)
Email Password   helpLost your password?
Desktop Development » Grid & Data Controls » Grid controls     Beginner License: The Code Project Open License (CPOL)

Check/uncheck CheckBox in a GridView using Javascript

By farazsk11

Check/uncheck CheckBox control inside a GridView using javascript without postback.
C# (C# 2.0, C#), Javascript, HTML, .NET (.NET, .NET 2.0), ASP.NET

Posted: 29 Mar 2008
Updated: 29 Mar 2008
Views: 26,959
Bookmarked: 16 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
13 votes for this Article.
Popularity: 4.96 Rating: 4.45 out of 5
1 vote, 7.7%
1
0 votes, 0.0%
2
2 votes, 15.4%
3
2 votes, 15.4%
4
8 votes, 61.5%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

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

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)

About the Author

farazsk11


I am a Software Engineer working for a company located in Sharjah, UAE. I like developing new applications with the latest technologies. Mostly reponsible for web applications using Microsoft.Net. I have done MCPD so far. Other than work play guitars, sing and play PSP.
Occupation: Web Developer
Location: United Arab Emirates United Arab Emirates

Other popular Grid & Data Controls articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 12 of 12 (Total in Forum: 12) (Refresh)FirstPrevNext
GeneralHow to Check Folder existancememberbachijvs3:39 29 Oct '08  
GeneralRe: How to Check Folder existancememberfarazsk114:04 29 Oct '08  
GeneralUn checking the header checkbox [modified]memberbachijvs22:26 9 Sep '08  
GeneralRe: Un checking the header checkboxmemberfarazsk1123:41 9 Sep '08  
GeneralRe: Un checking the header checkboxmemberbachijvs2:14 10 Sep '08  
GeneralMasterPage - this doesnt work ...memberdesi_nerd4:18 5 Aug '08  
GeneralRe: MasterPage - this doesnt work ...memberfarazsk1111:37 5 Aug '08  
GeneralRe: MasterPage - this doesnt work ...memberdesi_nerd23:34 5 Aug '08  
GeneralRe: MasterPage - this doesnt work ...memberfarazsk1122:16 6 Aug '08  
GeneralRe: MasterPage - this doesnt work ...memberdesi_nerd5:12 31 Aug '08  
GeneralRe: MasterPage - this doesnt work ...memberrperetz11:15 26 Sep '08  
GeneralImprovements to your JavaScriptmemberNick Taylor4:04 1 Apr '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 29 Mar 2008
Editor:
Copyright 2008 by farazsk11
Everything else Copyright © CodeProject, 1999-2008
Web17 | Advertise on the Code Project