Loading ...

Check/Uncheck checkboxes in Repeater using JavaScript | CodeAsp.Net

Check/Uncheck checkboxes in Repeater using JavaScript

 /5
0 (0votes)

Many times , we face a situation where we want to place checkboxes inside  repeater, datalist, gridview, etc. and we want all checkboxes are  check, uncheck on the client side. Here we discuss the solution using JavaScript.To implement this we take a data control Repeater.
Below is the javaScript  code use that:

<script type="text/javascript" language="javascript">                           
function CheckAll(checkAllBox )
{                                                                 
var frm = document.aspnetForm;                              
var ChkState=checkAllBox.checked;                  
for(i=0;i< frm.length;i++)                                
{                                                                
e=frm.elements[i];                                  
if(e.type=='checkbox' && e.name.indexOf('Id') != -1)
e.checked= ChkState ;                       
}                                                              
}

function CheckChanged()
{                                                                  
var frm = document.aspnetForm;                             
var boolAllChecked;                                       
boolAllChecked=true;                                      
for(i=0;i< frm.length;i++)                                
{                                                                
e=frm.elements[i];                                       
if ( e.type=='checkbox' && e.name.indexOf('Id') != -1 )
if(e.checked== false)                                 
{                                                            
boolAllChecked=false;                              
break;                                                   
}                                                             
}                                                                 
for(i=0;i< frm.length;i++)                                 
{                                                                 
e=frm.elements[i];                                        
if ( e.type=='checkbox' && e.name.indexOf('checkAll') != -1 )
{                                                          
if( boolAllChecked==false)                        
e.checked= false ;                               
else                                                  
e.checked= true;                                 
break;                                                   
}                                                            
}                                                           
}                                                             
</script>      

Below is Repeater design code:
<asp:Repeater runat="server" ID="rptSentMessages"onitemdatabound="rptSentMessages_ItemDataBound">
<HeaderTemplate>
<thead>
<tr>
<th>
<input type="checkbox" id="chkid" onclick="CheckAll(this);" runat="server" name="chkid" />
</th>
<th width="20%" align="left">
To
</th>
</tr>
</thead>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td width="5%" align="center">
<input type="checkbox" runat="server" id="chkId" onclick="CheckChanged();" checked='false' name="sentID" />
</td>
<td width="20%" align="left">
<%#Eval("ToUser") %>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>

This is whole process to check/uncheck check box in data representation control.

Hope it will help to you....

Comments (no comments yet)

Top Posts