Solved

C# not recognizing ListBox

Posted on 2011-02-18
5
667 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 53

Expert Comment

by:Huseyin KAHRAMAN
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Visual Studio 2015 auto inserted code 12 92
Connect Gridview column to Textbox in C# 2 55
What are MicroServices? 4 69
history of pages 7 55
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…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

710 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