Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

asp.net vb.net select multiple items in Listbox

Posted on 2009-04-14
9
Medium Priority
?
2,057 Views
Last Modified: 2012-06-27
How can select multiple items in a asp.net listbox
0
Comment
Question by:rflorencio
[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
  • 3
9 Comments
 
LVL 8

Expert Comment

by:arhame
ID: 24141240
There is a property on the listbox you can set the "Selection Mode" to multiple.
0
 
LVL 8

Expert Comment

by:arhame
ID: 24141278
Once you have that property set, you can retrieve the values of what was selected in code behind by looping through the listbox.


If Listbox1.SelectedIndex > - 1 Then
  Dim ctr as Integer
  For ctr=0 to Listbox1.Items.Count()-1
  If Listbox1.items(ctr).selected
  Response.Write("You highlighted " & Listbox1.items(ctr).Text & "<br />")
End if
Next
End If

Open in new window

0
 
LVL 8

Accepted Solution

by:
arhame earned 1600 total points
ID: 24141293
Oops, put an extra End If in there...


If Listbox1.SelectedIndex > - 1 Then
  Dim ctr as Integer
  For ctr=0 to Listbox1.Items.Count()-1
  If Listbox1.items(ctr).selected
  Response.Write("You highlighted " & Listbox1.items(ctr).Text & "<br />")
  Next
End If

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 15

Assisted Solution

by:spprivate
spprivate earned 400 total points
ID: 24141298
To allow users to select multiple values from a listbox, you need to set two properties, such as rows and SelectionMode. The value for rows can be any integer and for Selectionmode, it will be Multiple. To retrieve the all the values selected by the user, we need to make use of the properties such as Items.Count. And the property, selected will tell us whether the item have been selected or not. See the following example.

Listbox Web Server Control -

Sub mySub(sender As Object, e As EventArgs)
If lstStates.SelectedIndex > - 1
lblMsg.Text=""
Dim ctr as Integer
For ctr=0 to lstStates.Items.Count()-1
If lstStates.items(ctr).selected
lblMsg.Text += " You selected " & lstStates.items(ctr).Text & ""
end if
Next
End If
End Sub

ASP.NET Listbox Server Control

Ohio
Michigan
Wisconsin
Texas
Dallas
Indiana

0
 
LVL 8

Expert Comment

by:arhame
ID: 24141315
spprivate - if you're going to copy and paste somebody elses article I think it's common courtesy to at least put the link to it to give the original author the credit they're due.

http://www.coders2020.com/question/microsoft/asp-net/how-to-select-multiple-items-in-a-listbox-in-asp-net
0
 
LVL 15

Expert Comment

by:spprivate
ID: 24141365
thanks for the advise
0
 
LVL 15

Expert Comment

by:spprivate
ID: 24141382
Infact in all my post i used to do that.This one was handy with me the exact example so just posted on the fly
0
 
LVL 8

Expert Comment

by:arhame
ID: 24141411
Oh no biggie.  It's easier than writing your own code and half the time .net blog experts can explain it much better than I can so probably more helpful - I can definitely see why.  Just think the original authors should get the credit for the stuff they wrote is all :)  Happy coding.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

610 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