Aspnet Listbox Selectedindexchanged problems and solution

Aspnet Listbox Selectedindexchanged problems and solution

Asp.net-ListBox-SelectedIndexChanged SelectedIndexChanged is an event of ListBox in Asp.net. This example shows how to deal with Selected Index changed event. When post back occurs, make sure that you are not binding the listbox again. To avoid listbox binding, check IsPostBack property of the page and prevent binding. Here are common problems and solutions.

SelectedIndexChanged event Not working

Assuming you have set AutoPostBack property of ListBox to true and attached SelectedIndexChanged event. Even though your listBox selection is not functioning as per expectation. Solution: Makesure you are binding the listbox as shown in below code snippet.

protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
BindListBox();
}
}

You may have set the SelectedIndexChanged event to run code behind function. However, it's not working. Example:

<asp:ListBox ID="lstUsers" runat="server" OnSelectedIndexChanged="lstUsers_SelectedIndexChanged"></asp:ListBox>

protected void lstUsers_SelectedIndexChanged(object sender, EventArgs e)
{
}

Code will never work or selection will not cause post back.

SelectedIndexChanged and AutoPostBack property

The same code above will work and do post back. You need to set AutoPostBack property of ListBox to true.

<asp:ListBox ID="lstUsers"
             runat="server"
             OnSelectedIndexChanged="lstUsers_SelectedIndexChanged"
             AutoPostBack="True">
         </asp:ListBox>

 protected void lstUsers_SelectedIndexChanged(object sender, EventArgs e)
{
Label1.Text = lstUsers.SelectedItem.Text;
}

Tags:

listbox to listboxListBox Asp.net Binding csharplistbox select eventRemoving aspnet runtime from IISListbox select multipleC# Listbox Foreachasp.net add listBox itemsaspnet development server failed to start listening on portlearn aspnetaspnet 40 new featuresHigh performance aspnet application

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.