Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to find if the check box list is selected or not in asp.net

How to get selected index in a check box list in asp.net. Should I loop through to find whether the list box is selected or can i get to know without doing that. I want to do this

if(Checkboxlist selected) {do this} else {do this}

how to find if the check box list is selected or not in asp.net

int roleselected = ckl_EditRole.Items.SelectedIndex;
like image 355
John Avatar asked Nov 04 '11 13:11

John


People also ask

What is check box list in asp net?

ASP.NET CheckBoxList is a web control that can be used to collate the items that can be checked, thus giving the user the ability to select multiple items simultaneously. This list of items in the CheckBoxList can be dynamically generated using the Data Binding functions.


1 Answers

For CheckBoxList, SelectedIndex will give you just the first selected index in the CheckBoxList. If it's not -1, then something was selected. This may be enough for what you're looking for.

if( ckl_EditRole.SelectedIndex != -1 )
{
// Do Something
}

But, since the CheckBoxList can have multiple selections, you probably want to loop through the Items and look for the selected ones.

foreach( ListItem li in ckl_EditRole.Items )
{
    if( li.Selected )
    {
        // Do Something
    }
}
like image 171
Doozer Blake Avatar answered Oct 12 '22 10:10

Doozer Blake