Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListBox items not selecting (ASP/VB.NET)

I have a simple ASPX page with a listbox and a button. Listbox has about 8-10 items in it. After the user selects an item (listbox is multi-select) and clicks the button, I'm iterating through the items to get the selected one like so:

    For Each Item As ListItem In lstLetters.Items
        If Item.Selected Then
            Dim LetterID As String
            LetterID = Item.Value
            LetterIDs.Add(LetterID)
        End If
    Next

When I step through the code, I select the first item from the listbox. I setup a watch on the 'Item' variable. The code will iterate through each of the items -- but Item.Selected always reads 'False'.

I double-check the page, and sure enough my item is selected on the form.

What the heck is going on?

Thanks in advance,

Jason


1 Answers

Is it possible that maybe you're re-binding the listbox on each postback? That's a common error. Just make sure that you populate the control only if IsPostBack is false. Your code seems OK otherwise.

like image 134
kprobst Avatar answered Sep 16 '26 20:09

kprobst