Solved

C# not recognizing ListBox

Posted on 2011-02-18
5
638 Views
Last Modified: 2012-05-11
I have a ListBox in ASP.NET and when I attempt to call the SelectedItems.Count method, it throws me an error  


" 'System.Web.UI.WebControls.ListBox' does not contain a definition for 'SelectedItems' and no extension method 'SelectedItems' accepting a first argument of type 'System.Web.UI.WebControls.ListBox' could be found (are you missing a using directive or an assembly reference?)"
0
Comment
Question by:melli111
5 Comments
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34929210
There is no property 'SelectedItems' for ListBox
But 'SelectedItem' is there for ListBox
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 34929236
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listbox_properties.aspx

SelectedIndex : Gets or sets the lowest ordinal index of the selected items in the list. (Inherited from ListControl.) SelectedItem  : Gets the selected item with the lowest index in the list control. (Inherited from ListControl.)
SelectedValue : Gets the value of the selected item in the list control, or selects the item in the list control that contains the specified value. (Inherited from ListControl.)
0
 
LVL 15

Author Comment

by:melli111
ID: 34929378
The ListBox that I am using contains several selected values.  How would I access this in C# code?
0
 
LVL 23

Accepted Solution

by:
Rajkumar Gs earned 500 total points
ID: 34929424
Try this code - You need to loop through the items and check whether it is selected or not
foreach (ListItem item in ListBox1.Items)
        {
            if (item.Selected)
            {
                // This item is selected - do your stuff here
            }
        }

Open in new window

0
 
LVL 8

Expert Comment

by:Anurag Agarwal
ID: 34933215
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now