Return multiple selections in a listbox  asp.net 2.0

andrewh123
andrewh123 used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

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

Author

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

Commented:
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)
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

Author

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

Commented:
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
Commented:
And 1 more thing:
check for IsPostBack
   Sub Page_Load(sender As Object, e As EventArgs)
     If Not IsPostBack
     fill ur listbox with data otherwise it won't be really selected
          objListbox.DataSource = arrState
          objListbox.DataBind()
     End If
End Sub

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial