CheckBoxList Class
Creates a multi selection check box group that can be dynamically created by binding the control to a data source.
For a list of all members of this type, see CheckBoxList Members.
System.Object
System.Web.UI.Control
System.Web.UI.WebControls.WebControl
System.Web.UI.WebControls.ListControl
System.Web.UI.WebControls.CheckBoxList
[Visual Basic] Public Class CheckBoxList Inherits ListControl Implements IRepeatInfoUser, INamingContainer, _ IPostBackDataHandler [C#] public class CheckBoxList : ListControl, IRepeatInfoUser, INamingContainer, IPostBackDataHandler [C++] public __gc class CheckBoxList : public ListControl, IRepeatInfoUser, INamingContainer, IPostBackDataHandler [JScript] public class CheckBoxList extends ListControl implements IRepeatInfoUser, INamingContainer, IPostBackDataHandler
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Remarks
The CheckBoxList control provides a multi selection check box group that can be dynamically generated with data binding. It contains an Items collection with members corresponding to individual items in the list. To determine which items are checked, iterate through the collection and test the Selected property of each item in the list.
You can specify the way the list is displayed by using the RepeatLayout and RepeatDirection properties. If RepeatLayout is set to RepeatLayout.Table (the default setting), the list is rendered within a table. If it is set to RepeatLayout.Flow, the list is rendered without any table structure. By default, RepeatDirection is set to RepeatDirection.Vertical. Setting this property to RepeatDirection.Horizontal renders the list horizontally.
The CheckBoxList control provides convenient data binding capabilities. However, individual CheckBox controls provide greater control over layout.
CAUTION This control can be used to display user input, which might include malicious client script. Check any information that is sent from a client for executable script, SQL statements, or other code before displaying it in your application. ASP.NET provides an input request validation feature to block script and HTML in user input. Validation server controls are also provided to assess user input. For more information, see Validation Server Controls.
Example
[Visual Basic, C#] The following example demonstrates how to create a CheckBoxList control on a Web page.
[Visual Basic] <%@ Page Language="VB" AutoEventWireup="True" %> <html> <head> <script runat="server"> Sub Check_Clicked(sender as Object, e As EventArgs) Message.Text = "Selected Item(s):<br><br>" ' Iterate through the Items collection of the CheckBoxList ' control and display the selected items. Dim i As Integer For i=0 To checkboxlist1.Items.Count - 1 If checkboxlist1.Items(i).Selected Then Message.Text &= checkboxlist1.Items(i).Text & "<br>" End If Next End Sub </script> </head> <body> <form runat="server"> <h3> CheckBoxList Example </h3> Select items from the CheckBoxList. <br><br> <asp:CheckBoxList id="checkboxlist1" AutoPostBack="True" CellPadding="5" CellSpacing="5" RepeatColumns="2" RepeatDirection="Vertical" RepeatLayout="Flow" TextAlign="Right" OnSelectedIndexChanged="Check_Clicked" runat="server"> <asp:ListItem>Item 1</asp:ListItem> <asp:ListItem>Item 2</asp:ListItem> <asp:ListItem>Item 3</asp:ListItem> <asp:ListItem>Item 4</asp:ListItem> <asp:ListItem>Item 5</asp:ListItem> <asp:ListItem>Item 6</asp:ListItem> </asp:CheckBoxList> <br><br> <asp:label id="Message" runat="server"/> </form> </body> </html> [C#] <%@ Page Language="C#" AutoEventWireup="True" %> <html> <head> <script runat="server"> void Check_Clicked(Object sender, EventArgs e) { Message.Text = "Selected Item(s):<br><br>"; // Iterate through the Items collection of the CheckBoxList // control and display the selected items. for (int i=0; i<checkboxlist1.Items.Count; i++) { if (checkboxlist1.Items[i].Selected) { Message.Text += checkboxlist1.Items[i].Text + "<br>"; } } } </script> </head> <body> <form runat="server"> <h3> CheckBoxList Example </h3> Select items from the CheckBoxList. <br><br> <asp:CheckBoxList id="checkboxlist1" AutoPostBack="True" CellPadding="5" CellSpacing="5" RepeatColumns="2" RepeatDirection="Vertical" RepeatLayout="Flow" TextAlign="Right" OnSelectedIndexChanged="Check_Clicked" runat="server"> <asp:ListItem>Item 1</asp:ListItem> <asp:ListItem>Item 2</asp:ListItem> <asp:ListItem>Item 3</asp:ListItem> <asp:ListItem>Item 4</asp:ListItem> <asp:ListItem>Item 5</asp:ListItem> <asp:ListItem>Item 6</asp:ListItem> </asp:CheckBoxList> <br><br> <asp:label id="Message" runat="server"/> </form> </body> </html>
[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Namespace: System.Web.UI.WebControls
Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family
Assembly: System.Web (in System.Web.dll)
See Also
CheckBoxList Members | System.Web.UI.WebControls Namespace | CheckBox