I have a table cell that I fill with CheckBoxes and Labels at runtime.
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/>"));
}
}
Later, I'd like to loop through these Checkboxes to check what is Checked and what is not. I've tried various examples found on SO but with no luck. It's almost as if the checkboxes are not in the TableCell that I'v added them to. Both of the below loops do not find any checkbox controls:
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);
// }
//}