0

这是动态添加复选框的 HTML 代码

 <asp:Panel ID="Panel1" runat="server">
    <asp:Label ID="Label1" runat="server" Text="Text Catogaries"></asp:Label>
<br />

<br />
    <br />
    <asp:Repeater ID="Repeater1" runat="server">
        <ItemTemplate >

        <table>
      <td>
            <asp:CheckBox ID="CheckBox1" runat="server" Text='<%# Eval("CategoryName") %>'/>
            </td>
           </table>

        </ItemTemplate>

    </asp:Repeater>
    <br />
  <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    <br />
    <br />
</asp:Panel>

这是我在按钮后面用来检索复选框值的代码

 foreach (Control cr in Repeater1.Controls)
        {
            // controls within repeater item
            foreach (Control c in cr.Controls)
            {
                CheckBox chk = c as CheckBox;
                if (chk != null)
                {
                   list.Add(chk.text)
                }
            }

问题是如果我在列表中添加值的情况下,控制不会进入。如何在列表中添加价值

4

1 回答 1

0

使用下面的代码从转发器中查找复选框

foreach (RepeaterItem item in Repeater1.Items)
            {
                if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
                {
                    var checkBox1 = item.FindControl("CheckBox1") as CheckBox;
                    if (checkBox1 != null)
                    {
                        // Process further
                    }
                }
            }
于 2013-05-13T08:56:54.973 回答