简体   繁体   中英

ListBox selected index is lost after PostBack in ASP.NET

I've got an issue with a ListBox in ASP.NET application: after manual selection, an event "SelectedIndexChanged" fires, but its handler cannot keep neither the selected index nor the selected item. The index is always equal to '-1' :( I enabled AutoPostBack, so that the Text property of a TextBox could be updated.

In this code snippet, the SelectedItem is always null :(

protected void ListBox3_SelectedIndexChanged(object sender, EventArgs e)
{
    if (ListBox3.SelectedItem != null)
        TextBox6.Text = ListBox3.SelectedItem.Text;
}

Here is the description of a control:

<asp:ListBox ID="ListBox3" 
     runat="server" Height="283px" 
     style="margin-left: 28px; margin-top: 6px;" Width="156px" 
     DataSourceID="StacjeNaTrasie"
     DataTextField="Nazwa_Stacji" DataValueField="Nazwa_Stacji" 
     onselectedindexchanged="ListBox3_SelectedIndexChanged" 
     AutoPostBack="True" EnableViewState="True">
</asp:ListBox>

It looks like the PostBack triggers another selectionIndexChange event and the selected index is again set to '-1'. How could I keep the selected one?

Thank you for any help! :)

Are you sure its

onselectedindexchanged="ListBox2_SelectedIndexChanged" ?

isn't it suppose to be

onselectedindexchanged="ListBox3_SelectedIndexChanged" ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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