Individual CheckBox Web Controls ..HELP

G

Guest

I'm using three checkbox web controls in C# .NET

and one button, and one labe

Is there a way to "group" these individual checkbox web controls

If so, do I use a for loop, hashtable, 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(es) display 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 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

******
 
C

Chad Z. Hower aka Kudzu

=?Utf-8?B?YmVib3A=?= said:
Is there a way to "group" these individual checkbox web controls?

How about storing them in an array?

You could also loop through and find the list of checkboxes, but if you have
others you would have to separate them out.


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"


ELKNews - Get your free copy at http://www.atozedsoftware.com
 
S

Scott M.

Create one common event handler for all 3 controls by adding the controls
and their event to the "handles" clause of the routine. Then use the
"sender" argument to determine which checkbox fired the event.

I don't have the C# code for you but here it is in VB.NET:

Public Sub SomeCheckBoxGotClicked(sender as object, e as eventArgs) _
Handles chkBox1.checkChanged, chkBox2.checkChanged, chkBox3.checkChanged

'Since you know that only a checkbox could have fired this event, it's
ok to cast sender as a checkbox
'and look at some property that uniquely identifies it, such as its ID
Select Case CType(sender, System.Web.UI.Checkbox).ID
Case "chkBox1"
do something
Case "chkBox2"
do something
Case "chkBox3"
do something
End Select
End Sub


bebop said:
I'm using three checkbox web controls in C# .NET

and one button, and one label

Is there a way to "group" these individual checkbox web controls?

If so, do I use a for loop, hashtable, 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(es) display
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 out to the label.
Can this be done without using CheckBoxList web control?

Any suggestions would be appreciated.

Thanks.

bebop

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"
 

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

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top