ListBox sets to the first element when I selected another one

csnmgeek

New member
Joined
Dec 5, 2007
Messages
2
Programming Experience
Beginner
Hi everybody...
I have this problem...every time I select an item from a ListBox, the ListBox automatically selects the first item of the list...but this problem only happens when I execute this code:

VB.NET:
Partial Class _Default
    Inherits System.Web.UI.Page

    Dim ListBoxArray(3) As ListBox

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ListBoxArray(1) = ListBox1
        ListBoxArray(2) = ListBox2
        ListBoxArray(3) = ListBox3

        If Not IsPostBack Then
            Dim i As Integer
            For i = 1 To 4
                'this listbox doesnt reset
                ListBoxArray(1).Items.Add(i)
                
                'This two listboxes reset, but why?
                ListBoxArray(2).Items.Add(0)
                ListBoxArray(3).Items.Add(0)
        
            Next
        End If
    End Sub
   
End Class

HTML:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!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">
    <div>
    
        <asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True"></asp:ListBox>
        <asp:ListBox ID="ListBox2" runat="server" AutoPostBack="True"></asp:ListBox>
        <asp:ListBox ID="ListBox3" runat="server" AutoPostBack="True"></asp:ListBox>
    
    </div>
    </form>
</body>
</html>

But When I execute the following code, the listboxes doesn't reset to their first item every time I select another item

VB.NET:
Partial Class _Default
    Inherits System.Web.UI.Page

    Dim ListBoxArray(3) As ListBox

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ListBoxArray(1) = ListBox1
        ListBoxArray(2) = ListBox2
        ListBoxArray(3) = ListBox3

        If Not IsPostBack Then
            Dim i As Integer
            For i = 1 To 4
                'everything ok
                ListBoxArray(1).Items.Add(i)   
                ListBoxArray(2).Items.Add(i)
                ListBoxArray(3).Items.Add(i)
            Next
        End If
    End Sub
   
End Class

HTML:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!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">
    <div>
    
        <asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True"></asp:ListBox>
        <asp:ListBox ID="ListBox2" runat="server" AutoPostBack="True"></asp:ListBox>
        <asp:ListBox ID="ListBox3" runat="server" AutoPostBack="True"></asp:ListBox>
    
    </div>
    </form>
</body>
</html>

I don't know where the problem is... I hope you can help me
regards
jorge
 
In your first example, all the items for ListBox2 & ListBox3 have the same values. When a postback occurs, the selectedValue for the listbox is stored in viewstate. The first item having the same value as the stored selectedValue will be selected.
In the second example, the ListItems have different values and after a postback, the correct ListItem can be found from the SelectedValue stored in ViewState.
 
Back
Top