 
 
| 
 | ||||||||||||||||||||||||||
| 
 |   | 
 | ||||||||||||||||||||||||||||||||||||||||||
| 
 IntroductionThis article provides the code to add a checkbox in a  BackgroundI really had a very hard day Googling for code to check/uncheck all the checkboxes in a  Using the codeConsider the above image. I added a checkbox to the header template, and then each row of the  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  HistoryThis is my first post, hope this will be of interest to you. jebarson
 
 
 | ||||||||||||||||||||||||||||||||||||||||||||
| 
 | |||||||||||||||||||||||||
 General comment
 General comment     News / Info
 News / Info     Question
 Question     Answer
 Answer     Joke / Game
 Joke / Game     Admin message
 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 | 
|  | 
| The Ultimate Toolbox • ASP Alliance • Developer Fusion • Developersdex • DevGuru • Programmers Heaven • Planet Source Code • Tek-Tips Forums • | 
 
                     
                    
                 
                    
                 








 
 

 
 

 Search comments
 Search comments


 
  
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号