[RESOLVED] [2005] Set selected listbox items programmatically
Hi,
I'm trying to set up a multiple select list boxes selected items, but it's only displaying the last item. Does anyone know how to do this?
This is what I have -
vb Code:
...
Dim sSSect_Ids As String = clAct.DeNull(dr("SSection_Ids"))
If sSSect_Ids <> vbNullString Then
Dim sVals() As String = sSSect_Ids.Split(",".ToCharArray)
Dim i As Int32 = -1
Dim uBnd As Int32 = UBound(sVals)
For i = 0 To uBnd
sVals(i) = clAct.DeNull(sVals(i).Trim)
If sVals(i) <> vbNullString Then
Me.ddlSSection.SelectedValue = Convert.ToString(sVals(i))
End If
Next
End If
...
Al
Re: [2005] Set selected listbox items programmatically
I've managed to solve it, using .Items.FindByValue(s) and item(s).Selected=True -
vb Code:
Dim sSSect_Ids As String = clAct.DeNull(dr("SSection_Ids"))
If sSSect_Ids <> vbNullString Then
Dim sVals() As String = sSSect_Ids.Split(",".ToCharArray)
Dim i As Int32 = -1
Dim uBnd As Int32 = UBound(sVals)
For i = 0 To uBnd
sVals(i) = clAct.DeNull(sVals(i).Trim)
If sVals(i) <> vbNullString Then
sVals(i) = Convert.ToString(sVals(i))
If Not Me.ddlSSection.Items.FindByValue(sVals(i).ToString) Is Nothing Then
Me.ddlSSection.Items(sVals(i).ToString).Selected = True
End If
End If
Next
End If
Re: [RESOLVED] [2005] Set selected listbox items programmatically
Use the ASP.NET Listbox control. To read the selected items, use
vb Code:
For i = 0 to ListBox1.Items.Count
If ListBox1.Items(i).Selected = True Then
'Do something with ListBox1.Items(i)
End If
Next i
Re: [RESOLVED] [2005] Set selected listbox items programmatically
Mendhak,
Thanks for your reply, I'm not sure but think there might be a slight misunderstanding with my question. Your suggestion is exactly what I'm using to save the users selections to my DB. I was having trouble setting the default items (the items chosen previously) when a user edits a record
If you have a better way of achieving this then I'd be delighted to see it
Cheers Al
Re: [RESOLVED] [2005] Set selected listbox items programmatically
Ah yeah, your first post was a bit vague, so I jumped to the most common problem encountered with listboxes.
Your method is fine then, because all you need is some way to persist the values across postbacks or from elsewhere.