asp.net.ph

Skip Navigation Links

CheckBoxList Control Syntax

ASP.NET Syntax   ASP.NET Syntax for Web Controls


Creates a multi-selection check box group that can be dynamically generated by using data binding.

Declarative Syntax

For information on the individual members of this class, see CheckBoxList in the class library.

Remarks

The CheckBoxList control renders a multi-selection check box group that can be dynamically generated by using data binding. It contains an Items collection with members that correspond to individual items in the list. To determine which items are checked, loop through 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.

NOTE: You can also use multiple CheckBox controls. The CheckBoxList control is easier for creating a set of check boxes using data binding, while the individual CheckBox control gives you greater control over layout.

Syntax Example

The following shows a sample declaration for a CheckBoxList control in an .aspx file. The list contains items that are not mutually exclusive. In this case, the page is not immediately posted back to the server when a user checks one of the boxes, until some other event occurs, such as a Button click ).

<asp:CheckBoxList id="CheckList1" runat="server">

   <asp:ListItem>Item 1</asp:ListItem>
   ...
   <asp:ListItem>Item n</asp:ListItem>

</asp:CheckBoxList>

The following example shows how you can determine which check boxes are selected in a CheckBoxList control. The code loops through the control’s Items collection, and for each item, tests the item’s Selected property.

void Button1_onClick ( Object sender, EventArgs e ) {
   string s = "Selected items:<br>";

   for ( int i=0; i < CheckList1.Items.Count; i++ ) {
      if ( CheckList1.Items [ i ] .Selected ) {
         s += CheckList1.Items [ i ] .Text + "<br>";
      }
   }
   msgLabel.Text = s;
}
  C# VB

See Also

CheckBoxList Class   CheckBox and CheckBoxList Web Server Controls



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note