简体   繁体   English

列表框项目未选择(ASP / VB.NET)

[英]ListBox items not selecting (ASP/VB.NET)

I have a simple ASPX page with a listbox and a button. 我有一个带有列表框和按钮的简单ASPX页面。 Listbox has about 8-10 items in it. 列表框中包含大约8-10个项目。 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. 我在'Item'变量上设置了一个手表。 The code will iterate through each of the items -- but Item.Selected always reads 'False'. 代码将遍历每个项目 - 但Item.Selected总是读取'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. 只有在IsPostBack为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. 我遇到了类似的情况,但这是因为列表框的Enabled属性在其他代码中设置为False Until I set that to true , the selected index remained -1. 在我将其设置为true ,所选索引保持为-1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM