I have this code :
foreach (MyObject object in MyObject)
{
Literal checkBoxStart = new Literal();
Literal checkBoxEnd = new Literal();
checkBoxStart.Text = "<div class=\"item\">";
checkBoxEnd.Text = "</div>";
CheckBox chb = new CheckBox();
chb.InputAttributes.Add("value", object.UniqueID);
chb.Text = object.Title;
panelLocalita.Controls.Add(checkBoxStart);
panelLocalita.Controls.Add(chb);
panelLocalita.Controls.Add(checkBoxEnd);
}
than, on cmdSend_Click(object sender, EventArgs e) method, I’d like to browse the panel, and check only Checkboxs. How can I do it?
You can dig into the Linq tool box. There’s a few methods that work on IEnumerable and since ControlsCollection implements that interface you can use them directly on the collection. One of the methods suits your needs very nicely.
The extension method
OfType<TResult>()will iterate the collection and only return those elements that is of the provided type.to get all the checkboxes you could do as follows:
and you can then either iterate via a foreach if you wish for side effects such as setting all to checked
or if you need to grab information from them you can use more tools from the Linq tool box such as if you wanted to only grab those that are checked:
or to check that all are checked