Checking CheckBox and CheckBoxList Controls

Checking CheckBox and CheckBoxList Controls

In ASP.NET, the CheckBox control is used to allow users to select one or more options from a list of choices. When a user selects a CheckBox, it is checked (i.e., a checkmark is displayed inside it), and when the user deselects it, it is unchecked (i.e., the checkmark is removed).

Similarly, the CheckBoxList control is used to present users with a list of options to select from. It contains multiple CheckBox controls, each representing a single option. When the user selects one or more options, the corresponding CheckBox controls are checked.

To check if a CheckBox is checked or not, you can use its Checked property. For example, if you have a CheckBox control with ID “chkAgree”, you can check if it is checked with the following code:

if (chkAgree.Checked)

{

    // CheckBox is checked

}

else

{

    // CheckBox is not checked

}

To work with a CheckBoxList, you can use a loop to iterate over its ListItem controls and check their Selected property. For example:

foreach (ListItem item in chkListOptions.Items)

{

    if (item.Selected)

    {

        // This item is selected

    }

}

You can also use the SelectedItems property to get a collection of all selected items in the CheckBoxList:

foreach (ListItem item in chkListOptions.SelectedItems)

{

    // Do something with the selected item

}

Apply for ASP.NET Certification Now!!

https://www.vskills.in/certification/certified-aspnet-programmer

Back to Tutorial

Share this post
[social_warfare]
Functions Basics
User defined functions

Get industry recognized certification – Contact us

keyboard_arrow_up