CheckBoxList Web Server Control
CheckboxList web server control is the same as Checkbox control. Before reading this
article, please read my article on
CheckBox Web Server Control.
In this article we will see how to use the CheckboxList Web server control
- How to assign text and retrieve values from a CheckboxList control?
- How to go through all the elements inside a CheckBoxList?
- How to find the selected items in a CheckBoxList?
CheckboxList - How to assign a text to and how to retrieve the values?
CheckBoxList is almost equivalent to the Listbox web server control. The following
example explains how to declare a CheckboxList web server control and how to assign
and retrieve values depending upon the values selected by the user.
<HTML>
<HEAD>
<title>CheckboxList Web Server Control - by Das
</title>
<script language=vb runat=server>
Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim ctr As Integer
lblResult.Text = "The values for your selection are: "
For ctr = 0 To chkBoxList.Items.Count - 1
If chkBoxList.Items(ctr).Selected Then
lblResult.Text += chkBoxList.Items(ctr).Value & ", "
End If
Next
End Sub
</script>
</head>
<body style="font: 10pt verdana">
<form id="Form1" method="post" runat="server">
<asp:Label ID=lblHobby text="What are your hobbies?" Runat=server />
<asp:CheckBoxList ID=chkBoxList Runat=server>
<asp:ListItem Value=1>Music</asp:ListItem>
<asp:ListItem Value=2>Driving</asp:ListItem>
<asp:ListItem Value=3>Boating</asp:ListItem>
<asp:ListItem Value=4>Fishing</asp:ListItem>
<asp:ListItem Value=5>Eating</asp:ListItem>
</asp:CheckBoxList>
<asp:Button ID="btnSubmit" Text="Submit" OnClick="btnSubmit_Click" Runat=server />
<asp:Label ID="lblResult" ForeColor="#ff0000" Font-Bold="True" Runat=server />
</form>
</body>
</HTML>
|
Test this Script
The above example is very simple. There are two things to learn from the above example. First,
to create an item for checkbox, you have to use the <asp:ListItem> tag. Then we use the
attribute Value to assign values to the individual checkbox item. This is the same as
the ListBox Server control.
Conclusion
CheckBoxList controls are very useful control for getting inputs for multiple choice questions.
Send your comments to das@aspalliance.com
Back to Tutorial
|