locked
Moving selected items of one listbox over to another listbox.

    Question

  • Hi all.

    I have a form that has to asp.net listboxes. the first one has a list of items. The second one is initally blank.

    What I need to do is move any selected item from the first listbox over to the second.

    I have the SelectionMode property for the Listbox set to multiple.

    From all the my reading, I saw that you can write a for loop like

    for i as Integer = 0 to lstFG.SelectedItems.Count - 1
        lstBox2.Items.Add(New ListItem(lstFG.SelectedItem.Text, lstFG.SelectedValue))
        lstFG.items.Remove(lstBox2.SelectedItem)
    Next

    But I get the following error in Visual Studio

    'SelectedItems' is not a member of  System.Web.UI.WebControls.ListBox


    any help would be apprecited

    Thanks,
    Dean
    Friday, March 27, 2009 7:50 PM

Answers

  • Yes that property do not exist, and As Reed says, you can post the question in asp.net forum, but I will type in some code that might help you

    For i As Integer = lstFG.Items.Count - 1 To 0 Step -1 ' go other way since values are getting deleted 
                If lstFG.Items(i).Selected Then
                    LstBox2.Items.Add(New ListItem(lstFG.Items(i).Text, lstFG.Items(i).Value))
                    lstFG.Items.Remove(lstFG.Items(i))
                End If
            Next
            
    
    
    
    ' or go same way but delete later
            For i As Integer = lstFG.Items.Count - 1 To 0 Step -1 ' go other way since values are getting deleted 
                If lstFG.Items(i).Selected Then
                    LstBox2.Items.Add(New ListItem(lstFG.Items(i).Text, lstFG.Items(i).Value))
                End If
            Next
          
            For i As Integer = lstFG.Items.Count - 1 To 0 Step -1
                If lstFG.Items(i).Selected Then
                    lstFG.Items.Remove(lstFG.Items(i))
                End If
            Next
    
    
           
    
    
     'if your lstbox2 is multiselect and you want to preserve selection the
            For Each item As ListItem In lstFG.Items
                LstBox2.Items.Add(item)
            Next
    • Marked as answer by Xingwei Hu Wednesday, April 01, 2009 10:27 AM
    Saturday, March 28, 2009 8:38 AM

All replies

  • Hi Dean,

    For ASP.Net questions, please visit http://forums.asp.net for the official ASP.Net community forums.  These forums are for windows desktop applications.  As there are many differences between Desktop and Web development (the controls themselves for one) so you will need to ask people who are more familiar with web development.
    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"
    Friday, March 27, 2009 8:04 PM
  • Yes that property do not exist, and As Reed says, you can post the question in asp.net forum, but I will type in some code that might help you

    For i As Integer = lstFG.Items.Count - 1 To 0 Step -1 ' go other way since values are getting deleted 
                If lstFG.Items(i).Selected Then
                    LstBox2.Items.Add(New ListItem(lstFG.Items(i).Text, lstFG.Items(i).Value))
                    lstFG.Items.Remove(lstFG.Items(i))
                End If
            Next
            
    
    
    
    ' or go same way but delete later
            For i As Integer = lstFG.Items.Count - 1 To 0 Step -1 ' go other way since values are getting deleted 
                If lstFG.Items(i).Selected Then
                    LstBox2.Items.Add(New ListItem(lstFG.Items(i).Text, lstFG.Items(i).Value))
                End If
            Next
          
            For i As Integer = lstFG.Items.Count - 1 To 0 Step -1
                If lstFG.Items(i).Selected Then
                    lstFG.Items.Remove(lstFG.Items(i))
                End If
            Next
    
    
           
    
    
     'if your lstbox2 is multiselect and you want to preserve selection the
            For Each item As ListItem In lstFG.Items
                LstBox2.Items.Add(item)
            Next
    • Marked as answer by Xingwei Hu Wednesday, April 01, 2009 10:27 AM
    Saturday, March 28, 2009 8:38 AM