Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 736
  • Last Modified:

C# not recognizing ListBox

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
melli111
Asked:
melli111
1 Solution
 
Rajkumar GsSoftware EngineerCommented:
There is no property 'SelectedItems' for ListBox
But 'SelectedItem' is there for ListBox
0
 
HainKurtSr. System AnalystCommented:
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
 
melli111SharePoint Administrator / DeveloperAuthor Commented:
The ListBox that I am using contains several selected values.  How would I access this in C# code?
0
 
Rajkumar GsSoftware EngineerCommented:
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
 
Anurag AgarwalCommented:
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now