title: ListBox selected index is lost after PostBack in ASP.NET
body: <p>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.</p>
  
  <p>In this code snippet, the <strong>SelectedItem</strong> is always <strong>null</strong> :(</p>
  
  <pre><code>protected void ListBox3_SelectedIndexChanged(object sender, EventArgs e)
  {
      if (ListBox3.SelectedItem != null)
          TextBox6.Text = ListBox3.SelectedItem.Text;
  }
  </code></pre>
  
  <p>Here is the description of a control:</p>
  
  <pre><code>&lt;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"&gt;
  &lt;/asp:ListBox&gt;
  </code></pre>
  
  <p>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?</p>
  
  <p>Thank you for any help! :)</p>