Ken Cox [MS MVP]
2/17/2004 3:37:00 AM
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]
"bebop" <cwbp15@yahoo.com> wrote in message
news:B53B0EAD-7126-47ED-A2F5-6C890594DEC9@microsoft.com...
> 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"
> Text="Button"></asp:Button><asp:Label id="Label1" style="Z-INDEX: 105;
> LEFT: 176px; POSITION: absolute; TOP: 48px" runat="server"></asp:Label>
>
> *******
>