Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do we check whether any item in the listbox is selected in ASP.NET 2.0?

I have to do a for loop and check whether any of the items are checked if I want to know if any of the items are checked.

In C#, there is something like

listbox.SelectedItems.Count();

Is there any similar method for ASP.NET?

like image 387
william Avatar asked Dec 28 '10 06:12

william


People also ask

How do I know if a ListBox item is selected?

To determine the items that are selected, you can use the Selected property of the list box. The Selected property of a list box is an array of values where each value is either True (if the item is selected) or False (if the item is not selected).

Which method is used to know the option selected from a ListBox?

If you want to obtain the index position of the currently selected item in the ListBox, use the SelectedIndex property. In addition, you can use the SelectedIndices property to obtain all the selected indexes in a multiple-selection ListBox.

How do I get all items selected in ListBox?

You can use the ListBox. GetSelectedIndices method and loop over the results, then access each one via the items collection. Alternately, you can loop through all the items and check their Selected property.

Which of the following is the ListBox method that returns a value that determines whether a specified item on the list is selected?

GetSelected(Int32) Method.


2 Answers

you can see whether any item is selected by the below code:

if (listboxname.SelectedIndex == -1)
       MessageBox.Show("Please select an Item first!");
like image 149
Daniel Avatar answered Sep 22 '22 21:09

Daniel


Looks like you'll need to loop through them.

like image 37
Jonathan Wood Avatar answered Sep 24 '22 21:09

Jonathan Wood