简体   繁体   中英

How to retrieve number of checked items in checklistbox in ASP.NET

I've been trying this for about 30 minutes and can't find an answer. How do I retrieve the number of checked items in a checklistbox in ASP?

Everywhere else on the internet says to use cblList.CheckedItems but the CheckedItems property isn't showing up for me in the Intellisense? Am I forgetting to include something? It's driving me bonkers. I'm using VB.NET for this ASP assignment.

CheckedItems is a winforms property, the webforms CheckBoxList supports also multi-selection, but it does not provide a method or property to retrieve the selected items directly. But you could use this little LINQ query:

IEnumerable<ListItem> selectedItems = CheckBoxList1.Items.Cast<ListItem>()
 .Where(li => li.Selected);

VB.NET:

Dim selectedItems = From item In CheckBoxList1.Items.Cast(Of ListItem)()
                    Where item.Selected

If you don't want to use LINQ as commented, use a loop:

Dim selected = New List(Of ListItem)
For Each item As ListItem In CheckBoxList1.Items
    If item.Selected Then selected.Add(item)
Next
Dim numSelected = selected.Count

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM