Introduction & Demonstration
The CheckBoxList control renders a list of check boxes. The check boxes can be
rendered horizontally or vertically. Unlike the other List controls, a user
always can select multiple items when using a CheckBoxList control. For example,
the page given below contains a CheckBoxList control that renders its list items
in two columns.
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs)
For Each item As ListItem In cblProducts.Items
If (item.Selected) Then
lblProduct.Text &= "<li>" &
item.Text
End If
Next
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:CheckBoxList
id="cblProducts"
DataSourceID="SqlDataSource1"
DataTextField="TITLE"
DataValueField="ID"
RepeatColumns="2"
Runat="server" />
<p>
<asp:Button
id="btnSubmit"
Text="Submit"
OnClick="btnSubmit_Click"
Runat="server" />
</p>
<hr />
<asp:Label
id="lblProduct"
EnableViewState="false"
Runat="server" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$
ConnectionStrings:DatabaseConnectionString1 %>"
ProviderName="<%$
ConnectionStrings:DatabaseConnectionString1.ProviderName %>"
SelectCommand="SELECT
[ID], [TITLE] FROM [PRO_LIST]"></asp:SqlDataSource>
</div>
</form>
</body>
</html>
When we click the Submit button, the values of the Text property of any selected
check boxes are displayed in a Label control. The selected check boxes are
retrieved from the CheckBoxList control's Items property.
The CheckBoxList control includes three properties that affect its layout:
- RepeatColumns: The number of columns of check boxes to display.
- RepeatDirection: The direction in which the check boxes are rendered. Possible values are Horizontal and Vertical.
- RepeatLayout: Determines whether the check boxes are displayed in an HTML table. Possible values are Table and Flow.
Normally, a
CheckBoxList control renders its list items in an HTML table. When the
RepeatLayout property is set to the value Flow, the items are not rendered in a
table.
HAVE A GREAT CODING!