user2114177
user2114177

Reputation: 381

asp.net listbox on selectindexchanged doesn't fire an event

<asp:ListBox ID="list1" runat="server" Height="200" Style=" margin: 0"   OnSelectedIndexChanged="list1_SelectedIndexChanged">
            <asp:ListItem Selected="True" Value="1">1</asp:ListItem>
            <asp:ListItem Value="2">2</asp:ListItem>
            <asp:ListItem Value="3">3</asp:ListItem>
            <asp:ListItem Value="4">4</asp:ListItem>
        </asp:ListBox>

i have listbox in the updatePanel

but when i select something it doesn't fire an event what am doing wrong?

Upvotes: 3

Views: 7381

Answers (2)

Ravi Gadag
Ravi Gadag

Reputation: 15881

You need to Set AutoPostBack Property to True. then only Postback happens and your selected index event will fire.

<asp:ListBox AutoPostBack="true" ID="yourLIst" runat="server" Height="200" Style=" margin: 0"   OnSelectedIndexChanged="yourLIst_SelectedIndexChanged">

Upvotes: 10

Tim Schmelter
Tim Schmelter

Reputation: 460298

You have to set AutoPostBack to true (default is false):

<asp:ListBox AutoPostBack="true" ID="list1" runat="server" Height="200" Style=" margin: 0"   OnSelectedIndexChanged="list1_SelectedIndexChanged">
   ....

Also note that you have to databind the ListBox only on the initial load not on every postback, so use the IsPostBack property to check it:

protected void Page_Load(Object sender, EventArgs e)
{ 
    if(!IsPostBack)
        DataBindListBox();
}

Upvotes: 6

Related Questions