Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all selected items from asp.net ListBox

Anyone know of a smooth way to get all of the selected items in a listbox control by using extension methods?

And, please, spare me the argument of it's irrelevant as to how one gets such a list because in the end everything uses a loop to iterate over the items and find the selected ones anyway.

like image 746
Jagd Avatar asked Dec 02 '09 00:12

Jagd


People also ask

How get multiple values from ListBox in asp net?

* To do multiple Selections in the ListBox then just hold Ctrl key and select the items you want. In this demo, we are going to store the selected employee names that is selected from the ListBox to the database.

How to get multiple selected values and items from ListBox in c#?

Try this: var lst = listBox1. SelectedItems. Cast<DataRowView>(); foreach (var item in lst) { MessageBox.


1 Answers

var selected = yourListBox.Items.GetSelectedItems();
//var selected = yourDropDownList.Items.GetSelectedItems();
//var selected = yourCheckBoxList.Items.GetSelectedItems();
//var selected = yourRadioButtonList.Items.GetSelectedItems();

public static class Extensions
{
    public static IEnumerable<ListItem> GetSelectedItems(
           this ListItemCollection items)
    {
        return items.OfType<ListItem>().Where(item => item.Selected);
    }
}
like image 181
Rubens Farias Avatar answered Oct 09 '22 23:10

Rubens Farias