218

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.

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.

0

8 Answers 8

451

Set DropDownList AutoPostBack property to true.

Eg:

<asp:DropDownList ID="logList" runat="server" AutoPostBack="True" 
        onselectedindexchanged="itemSelected">
    </asp:DropDownList>
4
  • 3
    Well, that seems to have fixed it, thank you. Can you explain why?
    – gcode
    Commented Feb 5, 2011 at 15:05
  • 2
    @TGP1994 :The AutoPostBack property is used to set or return whether or not an automatic post back occurs Commented Feb 7, 2011 at 5:27
  • 2
    When am selecting dropdown list , its going to first value of list Commented Sep 26, 2012 at 10:17
  • 27
    @MuhamedShafeeq - If you're populating the list in the Page_Load method make sure you wrap the code in if(!IsPostback). This will prevent it from reloading the list and setting it back to the first item.
    – DaveK
    Commented Jun 24, 2014 at 13:16
84

try setting AutoPostBack="True" on the DropDownList.

0
46

I know its bit older post, but still i would like to add up something to the answers above.

There might be some situation where in, the "value" of more than one items in the dropdown list is duplicated/same. So, make sure that you have no repeated values in the list items to trigger this "onselectedindexchanged" event

2
  • Two of my values were duplicated and that was causing it to not fire the onselectedindexchanged event even though the postback was happening. Thanks for this!
    – Mike
    Commented Jan 13, 2019 at 19:49
  • the selected value of dropdown was having null in many items. please check in query that value is not null. Commented Jan 2, 2020 at 13:00
14

Add property ViewStateMode="Enabled" and EnableViewState="true" And AutoPostBack="true" in drop DropDownList

1
  • the ViewState properties are not needed, and the AutoPostBack was suggested in multiple answers years prior to this one.
    – TylerH
    Commented Mar 12, 2020 at 16:13
4

Also make sure the page is valid. You can check this in the browsers developer tools (F12)

In the Console tab select the correct Target/Frame and check for the [Page_IsValid] property

If the page is not valid the form will not submit and therefore not fire the event.

3
  • 1
    Up vote for HerbalMart. In my case the page was invalid but because I use the Telerik RadAjaxManager on my pages the error was not showing when I tested in my browser. When I commented out the entire RadAjaxManager the error presented itself and I was able to fix it. Commented Jul 26, 2017 at 11:46
  • That may be helpful for people having that problem, but that's like telling people whose car won't start when they turn the key "make sure your headlights work or you won't be able to see when driving at night". In other words, this doesn't answer the question being asked.
    – TylerH
    Commented Mar 12, 2020 at 16:16
  • @TylerH My answer is a solution to the same problem. 'The event doesn't fire' The accepted answer solves it at the server side. But the problem could also be on the client side 'page not valid'. In my case it was client side so i shared my experience. It also helped out some people as you see.
    – HerbalMart
    Commented May 1, 2021 at 14:59
3

For me answer was aspx page attribute, i added Async="true" to page attributes and this solved my problem.

<%@ Page Language="C#" MasterPageFile="~/MasterPage/Reports.Master"..... 
    AutoEventWireup="true" Async="true" %>

This is the structure of my update panel

<div>
  <asp:UpdatePanel ID="updt" runat="server">
    <ContentTemplate>

      <asp:DropDownList ID="id" runat="server" AutoPostBack="true"        onselectedindexchanged="your server side function" />

   </ContentTemplate>
  </asp:UpdatePanel>
</div>
1

Instead of what you have written, you can write it directly in the SelectedIndexChanged event of the dropdownlist control, e.g.

protected void ddlleavetype_SelectedIndexChanged(object sender, EventArgs e)
{
 //code goes here
}
1
  • 1
    Only if the AutoEventWireup setting on the aspx page is true, otherwise you need to do it manually, as per the original example.
    – user593806
    Commented Mar 18, 2015 at 13:17
0

In my case the issue was related to the texts in the value filed of the combobox, if the texts contains special characters like "\r\n" the event SelectedIndexChanged will not be fired , because once the page is compiled the created javascript will fail.

Not the answer you're looking for? Browse other questions tagged or ask your own question.