0
votes

I've seen many questions around this one, but none hitting it directly.

I put a listbox on a page and populate it with three items from an Access database. I have a button on that page that will extract several values including the selected item from the listbox. Or I want to anyway.

I can see the item selected in windows (highlighted) when I click the button, but when I try to select it no item is available as selected in the listbox. The ListBox1.SelectedIndex is alway -1.

Here is the code from the page:

<asp:ListBox ID="ListBox1" runat="server">
  <asp:ListItem Text="List1" />
  <asp:ListItem Text="List2" />
  <asp:ListItem Text="List3" />
</asp:ListBox>

Is there a property missing?

Here is the code from the code behind page: Public Function getDept() As String

    Dim dept As String

    If ListBox1.SelectedIndex > -1 Then
        dept = ListBox1.SelectedItem.Text
    Else
        dept = "CMS"
    End If

    Return dept
End Function

Please help, I have until about noon to figure this out.

1

1 Answers

0
votes

There may some reasons:

1- Check if your page's viewstate is true.

2- Call your method after Page_Load event.

Where do you call the function? Consider that you should call it after Page_Load event. Also your viewstate