How to get the selected-row count from a list box in asp.net
in开发者_运维知识库t countSelected = ListBoxMembers.Items.Cast<ListItem>().Where(i => i.Selected).Count();
string groupName = txt_GroupName.Text;
for (int counter = 0; counter < ListBoxMembers.Items.Count; counter++)
I have 20 items in the list, when I select only 2 ListBoxMembers.Items.Count shouws 20 and Countselected is 0
i tried this int count = ListBoxMembers.GetSelectedIndices().length; system.web.ui.controls.listbox does not contain a definition for selected items and no extension method selevted items acceptin first argument error
<asp:ListBox ID="ListBoxMembers" runat="server" SelectionMode="Multiple" CssClass="style102"
ToolTip="Press ctrl to select multiple users" DataValueField="FirstName"></asp:ListBox>
Just use the functionality built-in to the ListBox ... no Linq required. (For WinForms)
int countSelected = ListBoxMembers.SelectedItems.Count;
Edit: (since the OP added to the question and it appears it is regarding ASP.NET)
int countSelected = 0;
foreach(ListItem li in ListBoxMembers.Items)
if(li.Selected)
countSelected++;
I'm not sure why the Linq statement is not working. It certainly appears that it should be... is there any difference if you explicitly include true? i => i.Selected == true
Your code looks correct, but you can try this:
int count = ListBoxMembers.GetSelectedIndices().Length;
there should be somehting like listBoxMembers.SelectedItems which returns a list of the selected items. So you could do listBoxMembers.SelectedItems.count. Now this is in VB.Net but there should be something like it for C#
精彩评论