Click here to Skip to main content
Click here to Skip to main content

Client Side Validation for a Checkbox in DataGrid

, 8 Mar 2006
Rate this:
Please Sign up or sign in to vote.
This article shows how to use a checkbox in a DataGrid using JavaScript.

Introduction

This article provides the code to add a checkbox in a DataGrid, and thereby allows you to check or uncheck all the checkboxes in the DataGrid using JavaScript.

Background

I really had a very hard day Googling for code to check/uncheck all the checkboxes in a DataGrid using JavaScript. I had an option to do it on the server side, but to check all the records in a DataGrid on the server-side, I will have to reload the web page from the server; sounds crazy, doesn’t it? So finally, I sat down and wrote the following code and solved my problem. I post this because I found that many of you are also surfing for the same cause.

Using the code

Consider the above image. I added a checkbox to the header template, and then each row of the DataGrid will the checkbox. Checking or un-checking any of the checkboxes in the DataGrid will show the same functionality through out the DataGrid.

The HTML code for adding the checkbox looks like this:

//
// this html tag adds checkbox to header
//
<asp:TemplateColumn> 
 <HeaderTemplate> 
  <Input id="checkAll" type=checkbox 
    onclick="DGSelectOrUnselectAll('DataGrid1',this,'chkDel')" > 
 </HeaderTemplate>
 //
 // this html tag adds checkbox to datagrid
 <ItemTemplate> 
  <asp:CheckBox ID="chkDel" Runat="server"></asp:CheckBox> 
 </ItemTemplate> 
</asp:TemplateColumn>

Now, let's pass on to JavaScript for the check/uncheck functionality:

//-------------------------------------------------------
//this is to select or unselect the datagrid check boxes 

function DGSelectOrUnselectAll(grdid,obj,objlist){ 
//this function decides whether to check or uncheck all
    if(obj.checked) 
        DGSelectAll(grdid,objlist) 
    else 
        DGUnselectAll(grdid,objlist) 
} 
//---------- 
 
function DGSelectAll(grdid,objid){ 
//.this function is to check all the items
    var chkbox; 
    var i=2; 

    chkbox=document.getElementById(grdid + 
               '__ctl' + i + '_' + objid); 

    while(chkbox!=null){ 
        chkbox.checked=true; 
        i=i+1; 
        chkbox=document.getElementById(grdid + 
                   '__ctl' + i + '_' + objid); 
    } 

}//-------------- 

function DGUnselectAll(grdid,objid){ 
//.this function is to uncheckcheck all the items
    var chkbox; 
    var i=2; 

    chkbox=document.getElementById(grdid + 
               '__ctl' + i + '_' + objid); 

    while(chkbox!=null){ 
        chkbox.checked=false; 
        i=i+1; 
        chkbox=document.getElementById(grdid + 
                   '__ctl' + i + '_' + objid); 
    } 
}
//-------------------------------------------------------

That’s all, you are done. Now, you can check/uncheck all the checkboxes in the DataGrid using JavaScript.

History

This is my first post, hope this will be of interest to you.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

jebarson
Software Developer (Senior) Microsoft Corporation
India India
I work for Microsoft on MS technologies for application development. My interests include .net, WCF, Azure, Windows Phone, ASP.net, SL, WCF, WPF and many more.
 
You can visit my site at http://www.jebarson.info
 
Follow me on twitter @jebarson007
Follow on   Twitter

Comments and Discussions

 
Generalthanks PinmemberAnilMiLaN28-May-08 3:22 
Generalu r the best Pinmemberftoomi12-Jul-07 13:24 
AnswerRe: u r the best Pinmemberjebarson13-Jul-07 22:20 
Generalthanks Pinmemberahmadartoflove30-Jul-06 6:56 
Generalnice...but PinmemberSean Rock21-May-06 3:14 
GeneralThank you Pinmemberalob14-Mar-06 5:42 

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
Web04 | 2.8.141216.1 | Last Updated 8 Mar 2006
Article Copyright 2006 by jebarson
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid