Home > ASP.Net > Check all checkoxes in a Repeater

Check all checkoxes in a Repeater


Hi

try this example to Check all checkoxes in a repeater

<div id="Repeater1Div">
 <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
  <HeaderTemplate>
   <table width="100%" cellpadding="0" cellspacing="0">
   <tr>
    <td>
     <asp:CheckBox id="CheckAllCheckBox" Text="Check All" runat="server" />
    </td>
   </tr>
   </table>
  </HeaderTemplate>
  <ItemTemplate>
   <table width="100%" border="2" cellpadding="0" cellspacing="0">
    <tr>
     <td valign="top">
      <asp:Label id="Label1" Text='<%# Eval("SomeDatabaseField") %>' runat="server"></asp:Label>
     </td>
     <td valign="top">
      <asp:CheckBox ID="CheckBox1" runat="server" />
     </td>
    </tr>
   </table>
  </ItemTemplate>
 </asp:Repeater>
</div>

<script type="text/javascript">
<!--
function checkAllCheckBoxes(elementRef, parentId)
{
 var parentRef = document.getElementById(parentId);
 var inputElementArray = parentRef.getElementsByTagName('input');
 for (var i=0; i<inputElementArray.length; i++)
 {
  var inputElementRef = inputElementArray[i];
  if ( inputElementRef.type == 'checkbox' )
  {
   inputElementRef.checked = elementRef.checked;
  }
 }
 return false;
}
function setCheckAllCheckBox(elementRef, checkAllCheckBoxID)
{
 var checkAllCheckBoxRef = document.getElementById(checkAllCheckBoxID);
 if ( elementRef.checked == false )
  checkAllCheckBoxRef.checked = false;
 return false;
}
// -->
</script>

in code behind:

private string g_headerCheckBoxId = string.Empty;

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
 if (e.Item.ItemType == ListItemType.Header)
 {
  CheckBox headerCheckBox = (CheckBox)e.Item.FindControl("CheckAllCheckBox");
  if (headerCheckBox != null)
  {
   string containerId = "Repeater1Div";
   string clickHandler = string.Format("checkAllCheckBoxes(this, '{0}');", containerId);
   headerCheckBox.Attributes.Add("onclick", clickHandler);
   g_headerCheckBoxId = headerCheckBox.ClientID;
  }
 }
 else if ((e.Item.ItemType == ListItemType.AlternatingItem) || (e.Item.ItemType == ListItemType.Item))
 {
  CheckBox checkBoxRef = (CheckBox)e.Item.FindControl("CheckBox1");
  if (checkBoxRef != null)
  {
   string clickHandler = string.Format("setCheckAllCheckBox(this, '{0}');", g_headerCheckBoxId);
   checkBoxRef.Attributes.Add("onclick", clickHandler);
  }
 }
}

Hope this helps

Good Luck

Advertisements
Categories: ASP.Net
  1. RRaveen
    July 2, 2009 at 10:13 am

    Dear Friends,

    I hope you are doing well. I have launched a web site http://www.codegain.com and it is basically aimed C#,JAVA,VB.NET,ASP.NET,AJAX,Sql Server,Oracle,WPF,WCF and etc resources, programming help, articles, code snippet, video demonstrations and problems solving support. I would like to invite you as an author and a supporter. Looking forward to hearing from you and hope you will join with us soon.

    Please forward this email to all of your friends who are related IT. Send to us your feed about site also.

    Thank you
    RRaveen
    Founder CodeGain.com

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: