Yasserzaid’s Weblog

June 28, 2009

Check all checkoxes in a Repeater

Filed under: ASP.Net — yasserzaid @ 5:35 am

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

1 Comment »

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

    Comment by RRaveen — July 2, 2009 @ 10:13 am


RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.