title: selectedIndex is lost during postbacks - ASP.NET
body: <p>I have a list box control:</p>
  
  <pre>
  <code>
  &lt;asp:ListBox runat="server" id="lbox" autoPostBack="true" />
  </code>
  </pre>
  
  <p>The code behind resembles:</p>
  
  <pre>
  <code>
  private void Page_Load(object sender, System.EventArgs e)
  {
      lbox.SelectedIndexChanged+=new EventHandler(lbox_SelectedIndexChanged);
      if(!Page.IsPostBack)
      {
          LoadData();     
      }
  }
  private LoadData()
  {
      lbox.DataSource = foo();
      lbox.DataBind();
  }
  protected void lboxScorecard_SelectedIndexChanged(object sender, EventArgs e)
  {
      int index = (sender as ListBox).selectedIndex;
  }
  </code>
  </pre>
  
  <p>My problem is that when my page receives a post back (when a user makes a selection in the listbox), the selection always "jumps" to the first item in the listbox, so that the index variable in my callback function is always 0.</p>
  
  <p>Seems like this may be a viewstate problem? How can I fix it so that the selection index remains through the postback?</p>
  
  <p>There is no ajax going on, this is .NET 1.0.</p>
  
  <p>Thanks.</p>
  
  <p><strong>EDIT 1</strong> JohnIdol has gotten me a step closer, If I switch the datasource from my original DataTable to an ArrayList, then everything work properly...what would cause this?</p>
  
  <p><strong>Edit 2</strong> It turns out that my DataTable had multiple values that were the same, so that the indexes were treated as the same as all items with the same value...thanks to those who helped!</p>