Solved

C# not recognizing ListBox

Posted on 2011-02-18
5
654 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

813 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

16 Experts available now in Live!

Get 1:1 Help Now