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.
还有,拜托,别跟我说一个人如何得到这样一个列表是无关的,因为最终所有东西都使用一个循环来遍历这些项并找到所选的项。
3 个解决方案
#1
26
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);
}
}
#2
5
Extension method:
扩展方法:
public static List<ListItem> GetSelectedItems(this ListBox lst)
{
return lst.Items.OfType<ListItem>().Where(i => i.Selected).ToList();
}
You can call it on your listbox like:
你可以在你的列表框中这样命名:
List<ListItem> selectedItems = myListBox.GetSelectedItems();
You could also do the conversion using a 'Cast' on the list box items like:
你也可以在列表框中使用“Cast”进行转换:
return lst.Items.Cast<ListItem>().Where(i => i.Selected).ToList();
Not sure which will perform better OfType
or Cast
(my hunch is Cast
).
不确定哪一种会表现得更好(我的直觉是铸造)。
Edit based on Ruben's feedback for a generic ListControl method which would indeed make it much more useful extension method:
根据Ruben的反馈对一个通用的ListControl方法进行编辑,这确实会使它成为更有用的扩展方法:
public static List<ListItem> GetSelectedItems(this ListControl lst)
{
return lst.Items.OfType<ListItem>().Where(i => i.Selected).ToList();
}
#3
1
Hello i've created one solution for this problem in this post using VB.NET:
你好,我在这篇文章中使用VB.NET为这个问题创建了一个解决方案:
Getting all selected values from an ASP ListBox
从一个ASP列表框中获取所有选择的值
This code below is the same as the link above:
下面的代码与上面的链接相同:
Public Shared Function getSelectedValuesFromListBox(ByVal objListBox As ListBox) As String
Dim listOfIndices As List(Of Integer) = objListBox.GetSelectedIndices().ToList()
Dim values As String = String.Empty
For Each indice As Integer In listOfIndices
values &= "," & objListBox.Items(indice).Value
Next indice
If Not String.IsNullOrEmpty(values) Then
values = values.Substring(1)
End If
Return values
End Function
I hope it helps.
我希望它有帮助。
#1
26
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);
}
}
#2
5
Extension method:
扩展方法:
public static List<ListItem> GetSelectedItems(this ListBox lst)
{
return lst.Items.OfType<ListItem>().Where(i => i.Selected).ToList();
}
You can call it on your listbox like:
你可以在你的列表框中这样命名:
List<ListItem> selectedItems = myListBox.GetSelectedItems();
You could also do the conversion using a 'Cast' on the list box items like:
你也可以在列表框中使用“Cast”进行转换:
return lst.Items.Cast<ListItem>().Where(i => i.Selected).ToList();
Not sure which will perform better OfType
or Cast
(my hunch is Cast
).
不确定哪一种会表现得更好(我的直觉是铸造)。
Edit based on Ruben's feedback for a generic ListControl method which would indeed make it much more useful extension method:
根据Ruben的反馈对一个通用的ListControl方法进行编辑,这确实会使它成为更有用的扩展方法:
public static List<ListItem> GetSelectedItems(this ListControl lst)
{
return lst.Items.OfType<ListItem>().Where(i => i.Selected).ToList();
}
#3
1
Hello i've created one solution for this problem in this post using VB.NET:
你好,我在这篇文章中使用VB.NET为这个问题创建了一个解决方案:
Getting all selected values from an ASP ListBox
从一个ASP列表框中获取所有选择的值
This code below is the same as the link above:
下面的代码与上面的链接相同:
Public Shared Function getSelectedValuesFromListBox(ByVal objListBox As ListBox) As String
Dim listOfIndices As List(Of Integer) = objListBox.GetSelectedIndices().ToList()
Dim values As String = String.Empty
For Each indice As Integer In listOfIndices
values &= "," & objListBox.Items(indice).Value
Next indice
If Not String.IsNullOrEmpty(values) Then
values = values.Substring(1)
End If
Return values
End Function
I hope it helps.
我希望它有帮助。