Welcome to W3Courses
 Like Us on Facebook

Create a Checkbox List Web Control Using Asp.Net & C#

The CheckBoxList control creates a multiselection check box group. To specify items that you want to appear in the CheckBoxList control, place a ListItem element for each entry between the opening and closing tags of the CheckBoxList control. The CheckBoxList control can be dynamically generated using data binding as well.

In order to control the layout you can use RepeatDirection control and set it to either Horizontal or Vertical in order to place your checkboxes horizontally in a line or stack them vertically. While using the Horizontal value for RepeatDirection control, you can set RepeatColumns to any number, and the checkboxes will form another row after the RepeatColumn numerical value.

Following is the code to generate a list of checkboxes. Please include the following code in the form section of your webpage.

<asp:CheckBoxList id="shift" RepeatLayout="flow" runat="server" RepeatDirection="Horizontal" RepeatColumns="5">

The following code is used to get the value of the selected checkboxes. Place the following code in the onClick function of your webpage.

string strshift = "";
for (int i = 0; i < shift.Items.Count; i++)
        if (shift.Items[i].Selected)
                strshift = strshift + shift.Items[i].Text + " ";

The value of all checkboxes selected by the user will be stored in the string named strshift. The values will be separated by a space.

Following is the list of all the methods and their available values for the CheckBoxList control:


<asp:CheckBoxList id="CheckBoxList1"
     DataSource='<% databindingexpression %>'

   <asp:ListItem value="value" selected="True|False">