Q:

aspx ListBoxis not working (SelectedIndex)

I have this in my aspx page:
<asp:ListBox ID="ListBoxDelete" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ListBoxDelete_SelectedIndexChanged1"></asp:ListBox>

Code behind:
protected void Page_Load(object sender, EventArgs e){
   ListBoxDelete.Items.Clear();
   List<string> itens = new List<string>();
   for(var item in itens){
      ListBoxDelete.Items.Add(item);
   }
}

protected void ListBoxDelete_SelectedIndexChanged1(object sender, EventArgs e){
   int x = ListBoxDelete.SelectedIndex;
   doSomething(x);
}

My SelectedIndex is not working... If I'm not mistaken, the value is always -1. What's the problem?

A:

Change your code as follow and try
protected void Page_Load(object sender, EventArgs e){
if(!IsPostBack)
{
   ListBoxDelete.Items.Clear();
   List<string> itens = new List<string>();
   for(var item in itens){
      ListBoxDelete.Items.Add(item);
   }
}
}

