Displaying selected items at the top of the listbox

Discussion in 'ASP .Net' started by Guest, Mar 9, 2006.

  1. Guest

    Guest Guest

    Displaying selected items at the top of the listbox

    I am having two listboxes - one with ids and second with the related names.
    When user selects an item in one listbox, the corresponding item will be
    selected in the second listbox too. The problem is, when the user selects an
    item and it is in the middle of the list, the user is not able to know
    whether it is selected or not. Is there a way to display all the selected
    items (multiple selection is allowed) at the top of the listboxes? I am using
    datasource to bind the data to the listboxes.
    Guest, Mar 9, 2006
    1. Advertisements

  2. Untested, but this should be the right direction:

    For Each Thing as ListItem In ListOfThings.Items
    If Thing.Selected Then
    ListOfThings.Items.Insert(0, Thing)
    End If

    Note that databinding and postbacks may affect how this is executed, so pay
    attention to your page events. Also I'm not 100% sure offhand whether this
    will create a new ListItem object and leave the other behind, or if it will
    rearrange like you need (I think it will rearrange it).
    Elliot Rodriguez, Mar 9, 2006
    1. Advertisements

  3. Guest

    Guest Guest

    'find the selected item in the second List
    Dim LItem As ListItem = secondList.Items.FindByValue(ID)
    If LItem Is Nothing Then
    'add it at the very top
    secondList.Items.Insert(0, New ListItem(LItem.Value, LItem.Text))
    'remove the found item
    End If
    Guest, Mar 9, 2006
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.