3 回答

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

TA貢獻(xiàn)1866條經(jīng)驗(yàn) 獲得超5個(gè)贊
按照這里的建議,我添加了一個(gè)擴(kuò)展方法,以使用LINQ返回繼承自的任何類型的所選項(xiàng)目的列表。System.Web.UI.WebControls.ListControl
每個(gè)ListControl對(duì)象都有一個(gè)Itemstype屬性ListItemCollection。 ListItemCollection公開的集合ListItems,每個(gè)都有一個(gè)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)注
- 708 瀏覽
添加回答
舉報(bào)