ASP.NET - List Control - Checkbox

A checkbox list in ASP.NET is a control that presents a list of options, where the user can select multiple options. Each option is represented by a checkbox, which is a small square box that can be checked or unchecked by the user.

Events:

  • SelectedIndexChanged: This event is triggered when the selected checkboxes in the list change.

Methods:

  • Add: Adds a new checkbox to the list.
  • Clear: Removes all checkboxes from the list.
  • FindByText: Finds the checkbox with the specified text in the list.
  • FindByValue: Finds the checkbox with the specified value in the list.

Properties:

  • SelectedIndices: Gets a collection of the indices of the selected checkboxes in the list.
  • SelectedValues: Gets a collection of the values of the selected checkboxes in the list.
  • Items: Gets a collection of all the checkboxes in the list.

Example

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CheckboxListExample.aspx.cs" Inherits="WebApplication1.CheckboxListExample" %>
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Programming Languages</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <h2>Select Programming Languages</h2>
            <asp:CheckBoxList ID="LanguageList" runat="server">
                <asp:ListItem Text="C#" Value="C#" />
                <asp:ListItem Text="Java" Value="Java" />
                <asp:ListItem Text="Python" Value="Python" />
                <asp:ListItem Text="JavaScript" Value="JavaScript" />
                <asp:ListItem Text="PHP" Value="PHP" />
            </asp:CheckBoxList>
            <br />
            <br />
            <asp:Button ID="SubmitButton" runat="server" Text="Submit" OnClick="SubmitButton_Click" />
            <br />
            <br />
            <asp:Label ID="ResultLabel" runat="server" />
        </div>
    </form>
</body>
</html>

In the code-behind file, we can handle the Click event of the submit button and retrieve the selected languages from the checkbox list:

using System;
using System.Web.UI.WebControls;
namespace WebApplication1
{
    public partial class CheckboxListExample : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void SubmitButton_Click(object sender, EventArgs e)
        {
            string selectedLanguages = "";
            foreach (ListItem item in LanguageList.Items)
            {
                if (item.Selected)
                {
                    selectedLanguages += item.Value + ", ";
                }
            }
            if (selectedLanguages != "")
            {
                selectedLanguages = selectedLanguages.TrimEnd(' ', ',');
                ResultLabel.Text = "You selected: " + selectedLanguages;
            }
            else
            {
                ResultLabel.Text = "Please select at least one language.";
            }
        }
    }
}