我有一个表单元格,我在运行时用CheckBoxes和Labels填充。
List<string> lstUserPool = new List<string>();
DataTable dt = GetData("SELECT UserName FROM eData ORDER BY UserName;", "Data Source = lewcomp1\\COMPLIANCE; Initial Catalog = ComplianceData; Integrated Security = True;");
for (int i = 1; i < dt.Rows.Count; i++)
{
CheckBox cb = new CheckBox();
cb.ID = "cb" + dt.Rows[i]["Username"].ToString();
Label lbl = new Label();
lbl.ID = "lbl" + dt.Rows[i]["Username"].ToString();
lbl.Text = dt.Rows[i]["Username"].ToString();
lbl.Font.Size = new FontUnit("18px");
if (IsOdd(i))
{
cellUsersPoolLeft.Controls.Add(cb);
cellUsersPoolLeft.Controls.Add(lbl);
cellUsersPoolLeft.Controls.Add(new LiteralControl("<br/>"));
}
if (IsEven(i))
{
cellUsersPoolRight.Controls.Add(cb);
cellUsersPoolRight.Controls.Add(lbl);
cellUsersPoolRight.Controls.Add(new LiteralControl("<br/>"));
}
}
稍后,我想循环浏览这些Checkbox,检查Checked是什么,什么不是。我已经尝试了在SO上找到的各种例子,但没有运气。它几乎就像复选框不在TableCell中,我将它们添加到了。以下两个循环都找不到任何复选框控件:
foreach (Control ctl in cellUsersPoolRight.Controls)
{
if (ctl is CheckBox)
{
}
}
//foreach(var checkBox in cellUsersPoolRight.Controls.OfType<CheckBox>())
//{
// if (checkBox.Checked)
// {
// naz.Add(checkBox.ID);
// }
//}
答案 0 :(得分:0)
答案 1 :(得分:0)
我建议你在转发器中使用Repeater和Databind一个Checkbox和Label to ItemTemplate。这将很容易让您通过循环转发器获取数据。如果您需要其他数据,请在转发器中添加Hiddenfield来存储它们。
<asp:Repeater ID="UserRepeater" runat="server">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:CheckBox ID="UserCheckBox" runat="server" Checked='<%# Convert.ToBoolean(Eval("IsUserChecked")) %>' ToolTip='<%# Eval("UserId") %>'
onmouseover="title='';" />
</td>
<td>
<asp:Label ID="UserNameLabel" runat="server" Text='<%# Eval("UserName") %>'></asp:Label>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
然后在你的代码隐藏
//initialize repeater data
userRepeater.DataSource = dt;
userRepeater.DataBind();
然后在你的imagebutton_click的回发中你可以从转发器中获取项目
foreach (RepeaterItem ri in UserRepeater.Items)
{
CheckBox userCheckBox = ri.FindControl("UserCheckBox") as CheckBox;
}