如何通过动态添加的复选框检索值



以下是动态添加复选框

的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)
                }
            }

问题是控制不进入如果条件,我在列表中添加值。如何在列表中添加值

使用下面的代码查找中继器中的复选框

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
                    }
                }
            }

最新更新