?
Solved

asp.net vb.net select multiple items in Listbox

Posted on 2009-04-14
9
Medium Priority
?
2,052 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
Industry Leaders: 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 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

Industry Leaders: 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!

Question has a verified solution.

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

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…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

764 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