Tom Gullen
Tom Gullen

Reputation: 61729

ASP.net dynamic checkbox

I generate my check boxes dynamically:

for (int i = 0; i < dtCommon.Count; i++)
{
    CheckBox newBox = new CheckBox();
    newBox.Text = dtCommon[i].userName;
    newBox.CssClass = "cbox";
    if (dtCommon[i].isAlreadyRequired > 0 )
    {
        newBox.CssClass = "cbox highlighted";
        newBox.Checked = true;
    }
    ApprovalSelectPanel.Controls.Add(newBox);
}

And when the save button is pressed I call this function:

protected void SaveUsers(object sender, EventArgs e)
{

}

How do I know which check boxes the user has checked?!

Upvotes: 0

Views: 2213

Answers (2)

M.Ramadan
M.Ramadan

Reputation: 464

I think it is better to use CheckBoxList Within the ApprovalSelectPanel instead of add it in the runtime and in the runtime do the following

        CheckBoxList1.DataSource = dtCommon;
        CheckBoxList1.DataMember = "userName";
        CheckBoxList1.DataBind();

To know which one is selected do the following

          foreach(ListItem item in CheckBoxList1.Items)
            if (item.Selected)
            {
                //Do any action
            }

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

You could loop through the ApprovalSelectPanel.Controls and cast them back to the corresponding CheckBox type and verify the Checked property.

Upvotes: 1

Related Questions