3 回答
TA贡献1799条经验 获得超8个赞
foreach (ListItem item in CBLGold.Items)
{
if (item.Selected)
{
string selectedValue = item.Value;
}
}
TA贡献1866条经验 获得超5个赞
按照这里的建议,我添加了一个扩展方法,以使用LINQ返回继承自的任何类型的所选项目的列表。System.Web.UI.WebControls.ListControl
每个ListControl对象都有一个Itemstype属性ListItemCollection。 ListItemCollection公开的集合ListItems,每个都有一个Selected属性。
C夏普:
public static IEnumerable<ListItem> GetSelectedItems(this ListControl checkBoxList)
{
return from ListItem li in checkBoxList.Items where li.Selected select li;
}
Visual Basic:
<Extension()> _
Public Function GetSelectedItems(ByVal checkBoxList As ListControl) As IEnumerable(Of ListItem)
Return From li As ListItem In checkBoxList.Items Where li.Selected
End Function
然后,仅使用以下两种语言即可:
myCheckBoxList.GetSelectedItems()
- 3 回答
- 0 关注
- 678 浏览
添加回答
举报