locked
Loop through selected items in a listbox in vb RRS feed

  • Question

  • User-556137015 posted

    Hi Guys

    I am binding my listbox to a dataTable, the reason why i am using a listbox is for multiple selection, the user can select more that 1 item, when the user has selected there items, i need to save them into the db, line after line. heres my code, i cannot get the listbox to display all the selected items in my test page.

    for now in my test page, i just want to show the selected indexes of the items, and then in my next step i would want to add then indexes to the database, for now i just want to loop and display all the selected indexes.

     Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
            '1. Create a connection
            Dim conn As New MySqlConnection(ConfigurationManager.ConnectionStrings("MyConn").ConnectionString)
            Try
                Dim Adaptor As MySqlDataAdapter
                Dim ConnStr As String = ""
                ConnStr = "Select * from client "
                Adaptor = New MySqlDataAdapter(ConnStr, conn)
                Dim myTable As Data.DataTable = New Data.DataTable()
                Adaptor.Fill(myTable)
                conn.Open()
                ListBox1.DataSource = myTable
                ListBox1.DataBind()
    
            Catch ex As Exception
                Response.Write(ex.Message)
            Finally
                conn.Close()
            End Try
        End Sub
    
        Protected Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
            Dim li As ListItem
    
            For Each li In ListBox1.Items
                If li.Selected = True Then
                    Response.Write(ListBox1.SelectedItem)
                End If
    
            Next
        End Sub

    Thanks guys!


    Thursday, October 22, 2009 9:00 AM

Answers

  • User1315229986 posted

    Hi,

    try using following code

            For Each item As ListItem In listbox.Items
                If item.Selected Then
                    If selectedIndex = "" Then
                        selectedIndex = item.Value
                    Else
                        selectedIndex = selectedIndex & ", " & item.Value
                    End If
                End If
            Next
            Response.Write(selectedIndex)



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 23, 2009 12:01 AM

All replies

  • User1942730190 posted

    Why are you looping and writing on the page in selectedindexchange event.

    Do one thing, create a button and on click of that button, copy the code from this one and try. 

    Thursday, October 22, 2009 11:27 PM
  • User1315229986 posted

    Hi,

    try using following code

            For Each item As ListItem In listbox.Items
                If item.Selected Then
                    If selectedIndex = "" Then
                        selectedIndex = item.Value
                    Else
                        selectedIndex = selectedIndex & ", " & item.Value
                    End If
                End If
            Next
            Response.Write(selectedIndex)



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 23, 2009 12:01 AM