【问题标题】:How to access a checkBox inside a repeater Header?如何访问转发器标头内的复选框?
【发布时间】:2011-02-18 12:03:24
【问题描述】:

我有一个转发器,它在 headerTemplate 中有一个复选框

<asp:Repeater ID="myRepeater" runat="Server">
  <HeaderTemplate>
    <table>
      <tr>
        <th>
          <asp:CheckBox ID="selectAllCheckBox" runat="Server" AutoPostBack="true>
                         .
                         .
                         .

我想知道如何在后面的代码中获取该复选框的值。有什么想法吗?

【问题讨论】:

    标签: c# asp.net checkbox


    【解决方案1】:

    在您的 ItemDataBound 方法中调用 FindControl("checkboxID") 并转换为 Checkbox

    void myRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e) {
        if (e.Item.ItemType == ListItemType.Header) {
            CheckBox cb = (CheckBox)e.Item.FindControl("selectAllCheckBox");
            bool isChecked = cb.Checked;
        }
    }
    

    或者你可以随时这样做:

    CheckBox cb = (CheckBox)myRepeater.Controls.OfType<RepeaterItem>().Single(ri => ri.ItemType == ListItemType.Header).FindControl("selectAllCheckBox");
    

    【讨论】:

    • 第二个选项似乎不起作用。找不到复选框
    • 抱歉,第二种方法行不通 - 我要删除它。如果您想在页眉或页脚行中获取内容,则必须使用 ItemDataBound 事件(我的第一种方式) - 请参阅 stackoverflow.com/questions/495597/…
    猜你喜欢
    • 1970-01-01
    • 2014-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-26
    相关资源
    最近更新 更多