我在列的标题中使用了一个CheckBox,而且该库也是容器Cehckbox。
我想在Repeater的标题中使用chechBox,当选中This Control时,会检查行的复选框。
我该怎么做?
答案 0 :(得分:3)
最佳答案将涉及客户端javascript。您将使用controlName.clientID获取控件的ID,并将其编码为javascript ..
但这是天真的Dotnet唯一答案:
鉴于转发器:
<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>
代码隐藏:
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
结束班
答案 1 :(得分:1)
这可以通过jQuery轻松实现。假设您有由转发器生成的下表:
<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>
然后你可以为标题中的复选框指定一个事件处理程序,它将切换所有其他复选框:
$(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);
});
});
答案 2 :(得分:0)
以下是Bill回答的相关C#代码:
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;
}
}
答案 3 :(得分:0)
CheckBox Checkall = SendBoxrep.Controls[0].Controls[0].FindControl("Checkall") as CheckBox;