Avatar of andrewh123
andrewh123
 asked on

Return multiple selections in a listbox asp.net 2.0

Hi,
Probably a very simple solution but it is driving me mad.
In asp.net 2.0 vb.net, I have a list box which allows the user to select mulitple selections.  These are stored in a database as comma separated values.

When I come to display the result again later, how do I get the multiple selections to be selected?

I extract the data from the database and pop it into an array then loop through that:
FOR
    lstBox.SelectedValue = x
Next

But this only puts the last selection of the loop in.

Thanks in anticipation
Andrew
ASP.NET

Avatar of undefined
Last Comment
pashik

8/22/2022 - Mon
pashik

There is hint for u:
look for Property SelectedItems in ListBox.
andrewh123

ASKER
Thanks pashik,
I am trying to set multiple selected values - could you be a bit more specific with your answer as I can't get this to work.

thanks
pashik

Sorry, wrote a bit wrong Property.
Here is hint in C# which u can easliy make in VB.Net
u can try this:
In loop u can set selected items in such way:
listBox1.SetSelected(1,true)
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
andrewh123

ASKER
Hi again,
I'm using asp.net - there doesn't appear to be a property setselected for listboxes.
pashik

ok, try this code:
this code used to store values of mselect value in database
 Dim ListItemSelected As String
  For Each li In ListBoxCoachCategory.Items
      If li.Selected = True Then
        ListItemSelected = ListItemSelected & li.Value & ","
      End If
  Next


to highlight from database to  multiselect
where SelectedDisplayValue is the value from database(eg SelectedDisplayValue= 3,4,10)

For i = 0 To (objListbox.Items.Count - 1)

 If InStr(SelectedDisplayValue, objListbox.Items(i).Value) Then
             objListbox.Items(i).Selected = True
 End If

Next
ASKER CERTIFIED SOLUTION
pashik

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.