Solved

asp.net vb.net select multiple items in Listbox

Posted on 2009-04-14
9
2,028 Views
Last Modified: 2012-06-27
How can select multiple items in a asp.net listbox
0
Comment
Question by:rflorencio
  • 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 400 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
 
LVL 15

Assisted Solution

by:spprivate
spprivate earned 100 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

759 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

18 Experts available now in Live!

Get 1:1 Help Now