Solved

asp.net vb.net select multiple items in Listbox

Posted on 2009-04-14
9
2,034 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

809 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