Individual CheckBox Web Controls ..HELP

B

bebop

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

******
 
K

Ken Cox [Microsoft MVP]

One way is to loop through all of the controls in the form and catch the
ones that are checkboxes. When you find a checkbox, get its properties and
record the info. Here's a little sample in VB:

Private Sub Button1_Click _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
Dim cntrlTester As Control
Dim chkTester As CheckBox
Dim sb As New System.Text.StringBuilder
For Each cntrlTester In _
Page.Controls(1).Controls
If TypeOf cntrlTester Is _
System.Web.UI.WebControls.CheckBox Then
chkTester = cntrlTester
sb.Append(chkTester.ID & _
" : " & chkTester.Checked.ToString & _
"<br>")
End If
Next
Label1.Text = sb.ToString
End Sub


<form id="Form1" method="post" runat="server">
<P>
<asp:CheckBox id="CheckBox1" runat="server"
Text="CheckBox1"></asp:CheckBox></P>
<P>
<asp:CheckBox id="CheckBox2" runat="server"
Text="CheckBox2"></asp:CheckBox></P>
<P>
<asp:CheckBox id="CheckBox3" runat="server"
Text="CheckBox3"></asp:CheckBox></P>
<P>
<asp:Label id="Label1" runat="server"></asp:Label></P>
<P>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button></P>
</form>

Does this help?

Ken
Microsoft MVP [ASP.NET]
 
C

cw bebop

I implemented the code you provided and it does work, as far as, getting
the right checkbox that was selected. Except that the result(s) are

CheckBox1 : False
CheckBox2 : False
CheckBox3 : True //gets the checkbox selected but not the value of the
checkbox selected (Label should display the value of the checkbox
selected)

Problem is that I don't use VB I use C#

and I'm not sure how to use the VB code and transfer it over to C# code.

Any more suggestions would be helpful.

Thanks.

bebop
 
K

Ken Cox [Microsoft MVP]

You just need to use the Text property. Maybe someone can convert this to
C#?

Private Sub Button1_Click _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
Dim cntrlTester As Control
Dim chkTester As CheckBox
Dim sb As New System.Text.StringBuilder
For Each cntrlTester In _
Page.Controls(1).Controls
If TypeOf cntrlTester Is _
System.Web.UI.WebControls.CheckBox Then
chkTester = cntrlTester
sb.Append(chkTester.ID & _
" : " & chkTester.Checked.ToString & _
" : " & chkTester.Text & _
"<br>")
End If
Next
Label1.Text = sb.ToString
End Sub
 
K

Ken Cox [Microsoft MVP]

Kamel Patel (http://www.kamalpatel.net/) passes this VB to C# conversion
along using his tool. I haven't tested it.

Button1.Click += new System.EventHandler(Button1_Click);
private void Button1_Click(System.Object sender, System.EventArgs e){
Control cntrlTester ;
CheckBox chkTester ;
System.Text.StringBuilder sb = new System.Text.StringBuilder ;
foreach(cntrlTester in Page.Controls(1).Controls) {
if (cntrlTester is System.Web.UI.WebControls.CheckBox){
chkTester = cntrlTester ;
sb.Append(chkTester.ID + " : " + chkTester.Checked.ToString
+ "
: " + chkTester.Text + "<br>") ;
}
}
Label1.Text = sb.ToString ;
}
 
C

cw bebop

I tried testing the C# code below: I keep getting an alert/warning:

Type and identifier are both required in a foreach statement

I don't know what that means?

bebop
 
C

cw bebop

I couldn't convert the VB code from the http://www.kamalpatel.net page
using the online Demo version

I get a Server Error in '/' Application. Runtime Error.

There has to be a way to do this.

Three CheckBox web controls and display the checkbox value that's
selected to a label.

I'm still looking for something that may help.

Any more suggestions would be appreciated.

Thanks.

bebop
 
K

Ken Cox [Microsoft MVP]

Sorry, I'm totally lame when it comes to C#.

You might want to post the VB code in a C# group and ask some kind soul to
convert it for you?
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top