Selectedindex always returns value of -1

dejswa

Member
Joined
Jun 29, 2010
Messages
11
Programming Experience
1-3
Sorry for the basic inquiry, but I have struggled with this for a while and have no clues.

I am using VS 2008 to write a vb.net app that runs on Windows IIS Server 2008. I have done asp for years prior and am now learning .net.

I am trying to act upon a listbox selection.

This is my code:

listtest.aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="listtest.aspx.vb" Inherits="listtest" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server" method="post">
<div>
<%
lsttest.Items.Add("one")
lsttest.Items.Add("two")
lsttest.Items.Add("3")
%>
<asp:ListBox ID="lsttest" runat="server"></asp:ListBox>
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</form>
</body>
</html>

listtest.aspx.vb

Partial Class listtest
Inherits System.Web.UI.Page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Response.Write("selectedindex is " & lsttest.SelectedIndex & "<br/>")
Response.Write(TextBox1.Text)
End Sub

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

End Sub
End Class

You may run it here:

http://jhdpetro.com:89/listtest.aspx

Make a selection and enter some text into the textbox.

The selected index always returns -1 although the textbox data is available. In addition, the selected item in the listbox is cleared when the page refreshes.

Why can't I access the selected item is there a simple fix to this?

Thank you for reading!

dj
 
Last edited:
It has to do with the page life cycle. Where you are adding the items to the listbox is a little unusual (imo). What happens is when you first load the page the Page_Load event fires, then the html side loads and that is where your list is populated. When you click the button after making your selection, the postback occurs. So now the Page_Load fires, then the Button1_click event fires. At this time the html side has not loaded, the server knows there is suppose to be a ListBox control but it doesn't have any values in it until it finishes the postback and can get around to loading them.

So why don't the values stay there after the initial load of the page and the postback, aren't they stored in the ViewState?
No they would not be stored in viewstate, as the viewstate is saved before the html is loaded, and that is where you are populating the listbox.

Simple fix either make the listbox values part of the control

VB.NET:
Expand Collapse Copy
<asp:ListBox ID="ListBox1" runat="server" />
        <asp:ListItem>one</asp:ListItem>
        <asp:ListItem>two</asp:ListItem>
        <asp:ListItem>3</asp:ListItem>
    </asp:ListBox>

or add them in the Page_Load event instead
VB.NET:
Expand Collapse Copy
 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ListBox1.Items.Add("one")
        ListBox1.Items.Add("two")
        ListBox1.Items.Add("3")
    End Sub
 
Thanks so much for the excellent help. Now it is working.

I added the code to the page_load event and it is working. I had to add some additional code to page_load to prevent it from adding to the list with each postback otherwise it re-added the same members over and over again. In actual practice, this might not matter since I will be setting some session variables and redirecting to another page after the user has selected an item. But what I did was to insert an 'if' clause for the entire contents for the page_load routine:

if lstusers.item.count = 0 then . . .

so that it only executes the .add method on the first loading of the page.

I'm slowly starting to understand the workings of the .net system. Thank you again!
 
Back
Top