Solved

asp.net vb.net select multiple items in Listbox

Posted on 2009-04-14
9
2,040 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
Technology Partners: 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!

 
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
 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

726 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