Retrieve Multiple Values from Listbox

slash75

Member
Joined
Aug 25, 2010
Messages
11
Programming Experience
Beginner
I have a listbox and want to show all of the selected values in a label.
I have tried the code below but can't get more than one value to show.
I think this is something simple but can't figure it out. Can anyone point me in the right direction?

VB.NET:
<form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        <br />
        <br />
        <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple">
            <asp:ListItem>Monday</asp:ListItem>
            <asp:ListItem>Tuesday</asp:ListItem>
            <asp:ListItem>Wednesday</asp:ListItem>
            <asp:ListItem>Thursday</asp:ListItem>
        </asp:ListBox>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" />
        <br />
    
    </div>
    </form>


VB.NET:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        For Each item As ListItem In ListBox1.Items
            If item.Selected = True Then
                Label1.Text = item.Text
            End If
        Next

    End Sub
 
I got this working with the code below, just can't figure out how to show in the label ...

VB.NET:
For i As Integer = 0 To ListBox1.Items.Count - 1
            If ListBox1.Items(i).Selected Then
                Dim strselecteditems As String = ListBox1.Items(i).Text
                Response.Write(strselecteditems & " ")
                'Label1.Text = strselecteditems
            End If
        Next
 
Thanks for the example. That worked. For some reason the values keep showing every time the button is clicked.
I only want them to show once. I added the postback code below but must be doing something wrong.

If Page.IsPostBack Then

For i As Integer = 0 To ListBox1.Items.Count - 1
If ListBox1.Items(i).Selected Then
Dim strselecteditems As String = ListBox1.Items(i).Text
Label1.Text = Label1.Text & " " & strselecteditems
End If
Next

End If
 
I changed that line now nothing shows up when the button is clicked.

Protected Sub Page_Load(ByVal Sender As Object, ByVal e As System.EventArgs) Handles Me.Load

If Not IsPostBack Then

For i As Integer = 0 To ListBox1.Items.Count - 1
If ListBox1.Items(i).Selected Then
Dim strselecteditems As String = ListBox1.Items(i).Text
'Response.Write(strselecteditems & " ")
'Label1.Text = strselecteditems
Label1.Text = Label1.Text & " " & strselecteditems
End If
Next

End If

End Sub
 
Sorry, I am very new to VB.NET. Yes, I just want to show the selected items from the listbox in the label when the button is clicked.
I moved everything inside the button click but still nothing shows up when the button is clicked.

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

If Not IsPostBack Then

For i As Integer = 0 To ListBox1.Items.Count - 1
If ListBox1.Items(i).Selected Then
Dim strselecteditems As String = ListBox1.Items(i).Text
Label1.Text = Label1.Text & " " & strselecteditems
End If
Next

End If

End Sub
 
John is right. Postback event is always executed when you click a button.
Means the page is being loaded in response to a client postback all the time. Therefore, the code into the if statement will be never executed as it always return false.
Just remove that If statement and try again. It should work!
 
Yes, it works if I remove the postback statement, but the values keep getting posting to the page every time the button is clicked.
If I select Monday and click the button the label displays "Monday".
Then if I select Monday and Tuesday the label shows "Monday Monday Tuesday"
I want the label to refresh every time the button is clicked so it only shows what is currently selected from the list box.
That is what I can't figure out.
 
Back
Top