Client Side Validation for a Checkbox in DataGrid
Client Side Validation for a Checkbox in DataGrid
View our advertisers Advertise with us
Client Side Validation for a Checkbox in DataGrid
All Topics, .NET, C#, ASP.NET >> ASP.NET >> ASP.NET Controls

Client Side Validation for a Checkbox in DataGrid
By jebarson

This article shows how to use a checkbox in a DataGrid using JavaScript. 
  C#, JScript
Windows, .NET
ASP.NET, Win32, VS, ADO.NET
Dev
  Posted 8 Mar 2006 16:01
Client Side Validation for a Checkbox in DataGrid Articles by this author
969 views
Search:
Client Side Validation for a Checkbox in DataGrid Ultimate Combo
MFC tools $399
Client Side Validation for a Checkbox in DataGrid Send to a friend
My Profile
Client Side Validation for a Checkbox in DataGrid
Client Side Validation for a Checkbox in DataGrid My Settings
Client Side Validation for a Checkbox in DataGrid My Articles
Client Side Validation for a Checkbox in DataGrid My Bookmarks
Client Side Validation for a Checkbox in DataGrid Sign out
Client Side Validation for a Checkbox in DataGrid
 
Client Side Validation for a Checkbox in DataGrid

Client Side Validation for a Checkbox in DataGrid
Client Side Validation for a Checkbox in DataGridPrint Client Side Validation for a Checkbox in DataGridBroken Article? Client Side Validation for a Checkbox in DataGridBookmark Client Side Validation for a Checkbox in DataGridDiscuss
1 vote has for this article.
Client Side Validation for a Checkbox in DataGrid Client Side Validation for a Checkbox in DataGrid Client Side Validation for a Checkbox in DataGrid Client Side Validation for a Checkbox in DataGrid Client Side Validation for a Checkbox in DataGrid
Popularity: 0. Rating: 5 out of 5.

Client Side Validation for a Checkbox in DataGrid

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

jebarson


Click here to view jebarson's online profile.


Other popular articles:

[Top] Rate this Article for us!     PoorExcellent  
Client Side Validation for a Checkbox in DataGrid

Client Side Validation for a Checkbox in DataGrid
Client Side Validation for a Checkbox in DataGridFAQ  Noise tolerance    Client Side Validation for a Checkbox in DataGrid Search comments  
  View    Per page  
Client Side Validation for a Checkbox in DataGridNew Message Msgs 1 to 1 of 1 (Total: 1) (Refresh) First Prev Next     
Subject  Author  Date 
Client Side Validation for a Checkbox in DataGrid
Client Side Validation for a Checkbox in DataGrid  Thank you Client Side Validation for a Checkbox in DataGrid  alob  11hrs 11mins ago 
Client Side Validation for a Checkbox in DataGrid 
Thank you it helped solve a different problem i had.

 
[Reply][Email][View Thread][Get Link] 5 (out of 5)
Client Side Validation for a Checkbox in DataGrid
Last Visit: 21:21 Tuesday 14th March, 2006 First Prev Next     

Client Side Validation for a Checkbox in DataGrid General comment    Client Side Validation for a Checkbox in DataGrid News / Info    Client Side Validation for a Checkbox in DataGrid Question    Client Side Validation for a Checkbox in DataGrid Answer    Client Side Validation for a Checkbox in DataGrid Joke / Game    Client Side Validation for a Checkbox in DataGrid Admin message


All Topics, .NET, C#, ASP.NET >> ASP.NET >> ASP.NET Controls
Updated: 8 Mar 2006 16:01
Article content copyright jebarson, 2006
everything else Copyright © CodeProject, 1999-2006.
Web12 | Advertise on The Code Project | Privacy

Client Side Validation for a Checkbox in DataGrid
The Ultimate ToolboxASP AllianceDeveloper FusionDevelopersdexDevGuruProgrammers HeavenPlanet Source CodeTek-Tips Forums
Help!
Articles
Message Boards
StoreFront
Lounge
What is 'The Code Project'?
General FAQ
Post a Question
Site Directory
About Us
Latest
Most Popular
Search
Site Directory
Submit an Article
Update an Article
Article Competition
Visual C++ / MFC
(Managed) C++/CLI
C#
ASP.NET
VB.NET
Web Development
ATL / WTL / STL
COM
.NET Framework
SQL / ADO / ADO.NET
XML / XSL
OS / SysAdmin
Work Issues
Article Requests
Collaboration
General Discussions
Hardware
Suggestions
Get Togethers
The Soapbox

相关文章: