TGP1994 TGP1994 - 1 month ago 14
ASP.NET (C#) Question

DropDownList's SelectedIndexChanged event not firing

This seems to be a frequent issue around the web, although none of the answers seem to have helped.

I have a DropDownList object in my web page. When I click on it and select a different value, nothing happens, even though I have a function wired up to the

SelectedIndexChanged
event. I'll try and post my code here as orderly as possible:

First, the actual object's HTML code:

<asp:DropDownList ID="logList" runat="server"
onselectedindexchanged="itemSelected">
</asp:DropDownList>


And this is that function,
itemSelected
:

protected void itemSelected(object sender, EventArgs e)
{
Response.Write("Getting clicked; " + sender.GetType().ToString());
FileInfo selectedfile;
Response.Write("<script>alert('Hello')</script>");
foreach (FileInfo file in logs)
{
if (file.Name == logList.Items[logList.SelectedIndex].Text)
{
Response.Write("<script>alert('Hello')</script>");
}
}
}


None of the Responses appear, and that portion of JavaScript is never run. I've tried this on the latest 3.6 version of Firefox, as well as Internet Explorer 8. This is being served from a Windows Server 2003 R2 machine, running ASP.Net with the .NET Framework version 4.

If anyone can help, that would be great.

Answer

Set DropDownList AutoPostBack property to true.

Eg:

<asp:DropDownList ID="logList" runat="server" AutoPostBack="True" 
        onselectedindexchanged="itemSelected">
    </asp:DropDownList>