如何使用 C#2008 在 ASP.net 中的中继器标头中使用项目

发布于 2024-08-08 18:08:32 字数 104 浏览 1 评论 0原文

我在列标题中使用复选框,该复选框也可以是容器 Cehckboxes。

我想在中继器的标题中使用 chechBox,当选中此控件时,会选中行的复选框。

我该怎么做?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

笑着哭最痛 2024-08-15 18:08:32

最好的答案是客户端 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

结束类

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

逆光下的微笑 2024-08-15 18:08:32

这可以通过 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);
    });
});

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);
    });
});
月棠 2024-08-15 18:08:32

以下是 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;
    }
}

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;
    }
}
月野兔 2024-08-15 18:08:32
CheckBox Checkall = SendBoxrep.Controls[0].Controls[0].FindControl("Checkall") as CheckBox;
CheckBox Checkall = SendBoxrep.Controls[0].Controls[0].FindControl("Checkall") as CheckBox;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文