以下是动态添加复选框的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)
}
}
问题是如果我在List中添加值的条件,控制不进入。如何在列表中添加值。
答案 0 :(得分: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
}
}
}