Results 1 to 5 of 5

Thread: [RESOLVED] [2005] Set selected listbox items programmatically

  1. #1

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Resolved [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:
    1. ...
    2. Dim sSSect_Ids As String = clAct.DeNull(dr("SSection_Ids"))
    3. If sSSect_Ids <> vbNullString Then
    4.     Dim sVals() As String = sSSect_Ids.Split(",".ToCharArray)
    5.     Dim i As Int32 = -1
    6.     Dim uBnd As Int32 = UBound(sVals)
    7.     For i = 0 To uBnd
    8.         sVals(i) = clAct.DeNull(sVals(i).Trim)
    9.         If sVals(i) <> vbNullString Then
    10.             Me.ddlSSection.SelectedValue = Convert.ToString(sVals(i))
    11.         End If
    12.     Next
    13. End If
    14. ...

    Al

  2. #2

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Re: [2005] Set selected listbox items programmatically

    I've managed to solve it, using .Items.FindByValue(s) and item(s).Selected=True -

    vb Code:
    1. Dim sSSect_Ids As String = clAct.DeNull(dr("SSection_Ids"))
    2. If sSSect_Ids <> vbNullString Then
    3.     Dim sVals() As String = sSSect_Ids.Split(",".ToCharArray)
    4.     Dim i As Int32 = -1
    5.     Dim uBnd As Int32 = UBound(sVals)
    6.     For i = 0 To uBnd
    7.         sVals(i) = clAct.DeNull(sVals(i).Trim)
    8.         If sVals(i) <> vbNullString Then
    9.             sVals(i) = Convert.ToString(sVals(i))
    10.             If Not Me.ddlSSection.Items.FindByValue(sVals(i).ToString) Is Nothing Then
    11.                 Me.ddlSSection.Items(sVals(i).ToString).Selected = True
    12.             End If
    13.         End If
    14.     Next
    15. End If

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [RESOLVED] [2005] Set selected listbox items programmatically

    Use the ASP.NET Listbox control. To read the selected items, use

    vb Code:
    1. For i = 0 to ListBox1.Items.Count
    2. If ListBox1.Items(i).Selected = True Then
    3. 'Do something with ListBox1.Items(i)
    4. End If
    5. Next i

  4. #4

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    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

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width