Click here to Skip to main content
15,919,434 members
Articles / Web Development / ASP.NET
Article

Client Side Validation for a Checkbox in DataGrid

Rate me:
Please Sign up or sign in to vote.
4.50/5 (14 votes)
8 Mar 20061 min read 84.5K   50   6
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:

HTML
//
// 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:

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


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

Comments and Discussions

 
Generalthanks Pin
AnilMiLaN28-May-08 2:22
AnilMiLaN28-May-08 2:22 
Generalu r the best Pin
ftoomi12-Jul-07 12:24
ftoomi12-Jul-07 12:24 
AnswerRe: u r the best Pin
jebarson13-Jul-07 21:20
jebarson13-Jul-07 21:20 
Generalthanks Pin
ahmadartoflove30-Jul-06 5:56
ahmadartoflove30-Jul-06 5:56 
Generalnice...but Pin
Sean Rock21-May-06 2:14
Sean Rock21-May-06 2:14 
hi,

just thought it was a waste to have 2 separate methods that do virtually the same thing. So i used the following single method.

function doSelection(gridId, checked, itemId)
{
    var chkBox;
    var i=2;
    var zero = "0"+i;
      
    chkBox = document.getElementById(gridId+"_ctl0"+i+"_"+itemId);
    while (chkBox != null && chkBox != "undefined")
    {
        chkBox.checked = checked;
        ++i;
        // gridview has a 0 (zero) in front for rows less than 10
        zero = (i < 10) ? "0" : "";  
        chkBox = document.getElementById(gridId+"_ctl"+zero+i+"_"+itemId);
    }
}

and for the onclick i did this:
doSelection('GridView1',this.checked,'chkDelete');

cheers.
GeneralThank you Pin
alob14-Mar-06 4:42
alob14-Mar-06 4:42 

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.