"bebop" <cwbp15@yahoo.com> wrote in message
news:593E849A-B89F-4D12-A18D-2E755F261BEA@microsoft.com...
> Individual CheckBox Web Controls ..HELP
> I'm using three checkbox web controls in C# .NET, one button, and one
label
>
> Is there a way to "group" these individual checkbox web controls?
>
> If so, how can I iterate the checkboxes using a loop, array, etc.
>
> What I'm looking for is how to determine what checkbox was selected from
the three checkbox web controls and have the selected checkbox value(s)
displayed in a label.
>
> I don't want to use the following:
>
> if (CheckBox1.Checked == true)
> {
> //do something
> }
> if (CheckBox2.Checked == true)
> {
> //do something
> }
> and so forth
>
> What if I had 50 Individual Checkbox web controls I don't want to have the
above if statement 50 times in my code behind;
>
> I want to have something else-such as a loop that will iterate through the
Individual Checkbox controls to check which is selected and print the
selected checkbox value out to the label.
>
> Can this be done without using CheckBoxList web control?
If you want a loop, you can group your checkboxes together in a panel,
and then use something like this:
foreach (Control c in Panel1.Controls) {
if(c is CheckBox) {
CheckBox cb = (CheckBox)c;
// do something with cb
}
}
Another solution would be to set the OnCheckedChanged event of
each checkbox to the same event handler, and perform the action there.
In this case however, the code will only be executed for checkboxes
where the selection has been changed by the user.
If they were all off to begin with, maybe this is what you want.
The CheckBoxList is by far the easiest solution however.
--
Jos