Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > CheckBox web controls using a loop

Reply
Thread Tools

CheckBox web controls using a loop

 
 
bebop
Guest
Posts: n/a
 
      02-20-2004
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

******

 
Reply With Quote
 
 
 
 
Jos
Guest
Posts: n/a
 
      02-20-2004
"bebop" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
 
cw bebop
Guest
Posts: n/a
 
      02-22-2004

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





*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
Jos
Guest
Posts: n/a
 
      02-22-2004
"cw bebop" <(E-Mail Removed)> wrote in message
news:%23wxkJxN%(E-Mail Removed)...
>
> 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);

--

Jos


 
Reply With Quote
 
cw bebop
Guest
Posts: n/a
 
      02-22-2004

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




*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
Jos
Guest
Posts: n/a
 
      02-22-2004
cw bebop wrote:
> 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.

--

Jos


 
Reply With Quote
 
cw bebop
Guest
Posts: n/a
 
      02-22-2004
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




*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Checkbox - Dynamically assign id using for loop Raheel Sharif Ruby 0 09-27-2009 09:49 PM
Using Web.config's <system.web><pages><controls><add /></controls></pages></system.web> To Register UserControls Nathan Sokalski ASP .Net 5 01-10-2007 10:50 AM
Using Web.config's <system.web><pages><controls><add /></controls></pages></system.web> To Register UserControls Nathan Sokalski ASP .Net Web Controls 4 12-21-2006 02:50 AM
Using Web.config's <system.web><pages><controls><add /></controls></pages></system.web> To Register UserControls Nathan Sokalski ASP .Net Building Controls 4 12-21-2006 02:50 AM



Advertisments