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 selecte开发者_如何学Cd 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
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.
I had a similar situation happen to me, but it was because the listbox's Enabled
property was set to False
in other code. Until I set that to true
, the selected index remained -1.
精彩评论