Get all selected items from asp.net ListBox

后端 未结 3 764
梦毁少年i
梦毁少年i 2021-02-07 12:19

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

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-07 12:44

    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 GetSelectedItems(
               this ListItemCollection items)
        {
            return items.OfType().Where(item => item.Selected);
        }
    }
    

提交回复
热议问题