3 回答

TA貢獻1799條經(jīng)驗 獲得超8個贊
foreach (ListItem item in CBLGold.Items)
{
if (item.Selected)
{
string selectedValue = item.Value;
}
}

TA貢獻1866條經(jīng)驗 獲得超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 關(guān)注
- 703 瀏覽
添加回答
舉報