CheckBoxList Control in ASP.NET

CheckBoxList Control

CheckBoxList is generally used, when you want to select one or more options from given several choices. Most of the properties are same as RadioButtonList control, but the main difference is that you can select more than one item from CheckBoxList control.
The CheckBoxList control is easier for use, when you have set of options of checkboxes.

Example

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
public partial class ListControls : System.Web.UI.Page
{   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            List<string> educationList = new List<string>();
            educationList.Add("MBA");
            educationList.Add("MCA");
            educationList.Add("BE");
            educationList.Add("B.Tech");
            educationList.Add("B.Arch");
            educationList.Add("PHD");
            CheckBoxList1.DataSource = educationList;
            CheckBoxList1.DataBind();
        }
    }    
    protected void Button1_Click(object sender, EventArgs e)
    {
        StringBuilder sb = new StringBuilder();
        foreach (ListItem item in CheckBoxList1.Items)
        {
            if (item.Selected)
            {
                sb.Append("</br>"+item );
            }
        }
        Label1.Text ="You have selected :"+ sb.ToString();     
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (CheckBoxList1.RepeatDirection == RepeatDirection.Vertical)
        {
            CheckBoxList1.RepeatDirection = RepeatDirection.Horizontal;
        }
        else
        {
            CheckBoxList1.RepeatDirection = RepeatDirection.Vertical;
        }
    }    
}


checkboxlist control

When you click on the RepeatDirection button the layout will be changed as Horizontal. By default, RepeatDirection is set to RepeatDirection.Vertical.

checkboxlist horizontal