CheckBox web controls using a loop

B

bebop

Individual CheckBox Web Controls ..HELP
I'm using three checkbox web controls in C# .NET, one button, and one labe

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 somethin

and so fort

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

Any suggestions would be appreciated

Thanks

bebo

code

<asp:CheckBox id="CheckBox1" style="Z-INDEX: 101; LEFT: 40px; POSITION: absolute; TOP: 32px" runat="server
Text="aaa"></asp:CheckBox><asp:CheckBox id="CheckBox2" style="Z-INDEX: 102; LEFT: 40px; POSITION: absolute; TOP: 64px" runat="server
Text="bbb"></asp:CheckBox><asp:CheckBox id="CheckBox3" style="Z-INDEX: 103; LEFT: 40px; POSITION: absolute; TOP: 96px" runat="server
Text="ccc"></asp:CheckBox><asp:Button id="Button1" style="Z-INDEX: 104; LEFT: 40px; POSITION: absolute; TOP: 144px" runat="server
Text="Button"></asp:Button><asp:Label id="Label1" style="Z-INDEX: 105; LEFT: 176px; POSITION: absolute; TOP: 48px" runat="server"></asp:Label

******
 
J

Jos

bebop said:
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.
 
C

cw bebop

Hi Jos

Your response is close to what I'm looking for.

This is what I tried:

HTML code:

<form id="Form1" method="post" runat="server">
<asp:CheckBox id="CheckBox1" style="Z-INDEX: 101; LEFT: 48px; POSITION:
absolute; TOP: 32px" runat="server"
Text="aaa"></asp:CheckBox>
<asp:CheckBox id="CheckBox2" style="Z-INDEX: 102; LEFT: 48px; POSITION:
absolute; TOP: 64px" runat="server"
Text="bbb"></asp:CheckBox>
<asp:CheckBox id="CheckBox3" style="Z-INDEX: 103; LEFT: 48px; POSITION:
absolute; TOP: 96px" runat="server"
Text="ccc"></asp:CheckBox>
<asp:Button id="Button1" style="Z-INDEX: 104; LEFT: 48px; POSITION:
absolute; TOP: 128px" runat="server"
Text="Button"></asp:Button>
<asp:Label id="Label1" style="Z-INDEX: 105; LEFT: 160px; POSITION:
absolute; TOP: 40px" runat="server"></asp:Label>
</form>
************

C# code behind:

private void Button1_Click(object sender, System.EventArgs e)
{
CheckBox cb;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach(Control c in Page.Controls[1].Controls)
{
if (c is CheckBox)
{
cb = (CheckBox)c;
CheckBox cb = (CheckBox)c;
//sb.Append(cb.ID + " : " + cb.Checked.ToString() + " : " + cb.Text +
"<br>");
sb.Append(cb.Checked.ToString());

}
}
Label1.Text = sb.ToString();

}

**********
When running the web app I checked the first checkBox and get the
result: TrueFalseFalse

Which is correct except that I want the value of the checkBox displayed
in Label1

Any ideas?

bebop
 
J

Jos

cw bebop said:
Hi Jos

Your response is close to what I'm looking for.

This is what I tried:

HTML code:

<form id="Form1" method="post" runat="server">
<asp:CheckBox id="CheckBox1" style="Z-INDEX: 101; LEFT: 48px; POSITION:
absolute; TOP: 32px" runat="server"
Text="aaa"></asp:CheckBox>
<asp:CheckBox id="CheckBox2" style="Z-INDEX: 102; LEFT: 48px; POSITION:
absolute; TOP: 64px" runat="server"
Text="bbb"></asp:CheckBox>
<asp:CheckBox id="CheckBox3" style="Z-INDEX: 103; LEFT: 48px; POSITION:
absolute; TOP: 96px" runat="server"
Text="ccc"></asp:CheckBox>
<asp:Button id="Button1" style="Z-INDEX: 104; LEFT: 48px; POSITION:
absolute; TOP: 128px" runat="server"
Text="Button"></asp:Button>
<asp:Label id="Label1" style="Z-INDEX: 105; LEFT: 160px; POSITION:
absolute; TOP: 40px" runat="server"></asp:Label>
</form>
************

C# code behind:

private void Button1_Click(object sender, System.EventArgs e)
{
CheckBox cb;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach(Control c in Page.Controls[1].Controls)
{
if (c is CheckBox)
{
cb = (CheckBox)c;
CheckBox cb = (CheckBox)c;
//sb.Append(cb.ID + " : " + cb.Checked.ToString() + " : " + cb.Text +
"<br>");
sb.Append(cb.Checked.ToString());

}
}
Label1.Text = sb.ToString();

}

**********
When running the web app I checked the first checkBox and get the
result: TrueFalseFalse

Which is correct except that I want the value of the checkBox displayed
in Label1

Any ideas?

bebop

I guess you want to know which checkboxes are checked?

Instead of
sb.Append(cb.Checked.ToString());

Try:
if (cb.Checked) sb.Append(cb.ID);
 
C

cw bebop

I did the following:

C# code behind:

private void Button1_Click(object sender, System.EventArgs e)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach(Control c in Page.Controls[1].Controls)
{
if (c is CheckBox)
{
CheckBox cb = (CheckBox)c;

if (cb.Checked) sb.Append(cb.ID);
}
}
Label1.Text = sb.ToString();
}
******

It does work when displaying the selected CheckBox control except that
it didn't display the CheckBox's text value.

I changed where it reads

if (cb.Checked) sb.Append(cb.ID)

TO

if (cb.Checked) sb.Append(cb.Text)

That did the trick. It works. I wanted to display the text value using
CheckBox web Controls instead of CheckBoxList.

I have question..

If you look at the code behind where the if statement is:

{
if (c is CheckBox)
{
CheckBox cb = (CheckBox)c;

if (cb.Checked) sb.Append(cb.ID);
}

I'm wondering will the nested if statement cause any problems or
exceptions later on?

It looks strange that I can put an if statement within an if statement.
I'm not used to seeing this.

Does this code look all right and won't cause problems later on (ie.
exception is thrown)

What do you think?

bebop
 
J

Jos

cw said:
I have question..

If you look at the code behind where the if statement is:

{
if (c is CheckBox)
{
CheckBox cb = (CheckBox)c;

if (cb.Checked) sb.Append(cb.ID);
}

I'm wondering will the nested if statement cause any problems or
exceptions later on?

It looks strange that I can put an if statement within an if
statement. I'm not used to seeing this.

Does this code look all right and won't cause problems later on (ie.
exception is thrown)

No, no problem at all.
Nested if's are quite common.
Actually, one of the purposes of exceptions is to catch the
exception, no matter how many levels of loops or conditions.
 
C

cw bebop

Thank you Jos for your help.

Problem: to see if Individual CheckBox web controls could be grouped and
whichever is selected displayed in a Label.

Solved. Thanks again.

bebop
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top