views:

199

answers:

4

hi I Use a CheckBox in Header of Column and that coulmn is Container Cehckboxes too .

I want to Use a chechBox in header in Repeater , and when This Control is Checked ,checkboxes of rows is checked .

How do I do This?

+1  A: 

This can be easily achieved with jQuery. Suppose you had the following table, generated by your repeater:

<table>
  <thead>
      <tr>
          <th><input type="checkbox" name="headerchk" id="headerchk" /></th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><input type="checkbox" name="chk1" id="garbage1_added_my_repeater_chk" /></td>
      </tr>
      <tr>
          <td><input type="checkbox" name="chk2" id="garbage2_added_my_repeater_chk" /></td>
      </tr>
      <tr>
          <td><input type="checkbox" name="chk3" id="garbage3_added_my_repeater_chk" /></td>
      </tr>
  </tbody>
</table>

Then you can assign an event handler for the checkbox in the header which will toggle all the other checkboxes:

$(function() {
    $('#headerchk').change(function() {
        // Toggle all inputs of type checkbox and with ids starting with chk:
        $('input[type=checkbox][id$=chk]').attr('checked', this.checked);
    });
});
Darin Dimitrov
A trifle note, sometimes there is a problem with NamingContainer, it's better to add a permanent thing in the end of element id and use $('input[type=checkbox][id$=foo]') instead.
Mehdi Golchin
@Mehdi, you are absolutely right about this. Usually the garbage is added at the beginning of identifiers. I will modify my post.
Darin Dimitrov
+3  A: 

The best answer would involve client-side javascript. You would get the id of the controls using controlName.clientID and have it coded into the javascript..

But here is the naive Dotnet only answer:

Given the Repeater:

    <asp:Repeater ID="Repeater1" runat="server">
    <HeaderTemplate>
       <div> <asp:CheckBox ID="CheckBox1" runat="server" OnCheckedChanged="SetAllChecks" AutoPostBack="true" /></div>
    <hr />
    </HeaderTemplate>
    <ItemTemplate>
       <div> <asp:CheckBox ID="CheckBox2" Checked='<%# Container.DataItem("isChecked") %>' runat="server" /></div>
    </ItemTemplate>
    </asp:Repeater>

And the codebehind:

Imports System.Data  
Imports System.Collections

Partial Class _Default  
Inherits System.Web.UI.Page  

Public Sub SetAllChecks(ByVal sender As Object, ByVal e As System.EventArgs)  
    Dim amIChecked As CheckBox = CType(sender, CheckBox)  

    Dim rowCt As Integer = Repeater1.Items.Count  
    Dim ridx As Integer = 0  
    For ridx = 0 To rowCt - 1  
        Dim cbox As CheckBox = CType(Repeater1.Items(ridx).FindControl("CheckBox2"), CheckBox)  
        cbox.Checked = amIChecked.Checked  
    Next  

End Sub  

Public Sub Repeater1_OnItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Repeater1.ItemDataBound

End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
        Dim dt As New DataTable()
        dt.Columns.Add(New DataColumn("isChecked"))
        Dim tmprow As DataRow = dt.NewRow()
        tmprow("isChecked") = True
        dt.Rows.Add(tmprow)
        tmprow = dt.NewRow()
        tmprow("isChecked") = False
        dt.Rows.Add(tmprow)
        tmprow = dt.NewRow()
        tmprow("isChecked") = False
        dt.Rows.Add(tmprow)
        tmprow = dt.NewRow()
        tmprow("isChecked") = False
        dt.Rows.Add(tmprow)
        tmprow = dt.NewRow()
        tmprow("isChecked") = False
        dt.Rows.Add(tmprow)

        Repeater1.DataSource = dt
        Repeater1.DataBind()

    End If

End Sub

End Class

Bill Bingham
My code is C# no VB
mohammad reza
A: 

Here is the relevant C# code from Bill's answer:

public void SetAllChecks(object sender, EventArgs e)
{
    CheckBox amIChecked = (CheckBox)sender;
    foreach (RepeaterItem ri in Repeater1.Items)
    {
        CheckBox cbox = (CheckBox)ri.FindControl("CheckBox2");
        cbox.Checked = amIChecked.Checked;
    }
}
Jason Berkan
A: 
CheckBox Checkall = SendBoxrep.Controls[0].Controls[0].FindControl("Checkall") as CheckBox;
mohammad reza