DropDownList's SelectedIndexChanged event not firing
Asked Answered
S

8

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.

Sense answered 5/2, 2011 at 6:1 Comment(0)
A
449

Set DropDownList AutoPostBack property to true.

Eg:

<asp:DropDownList ID="logList" runat="server" AutoPostBack="True" 
        onselectedindexchanged="itemSelected">
    </asp:DropDownList>
Attired answered 5/2, 2011 at 6:6 Comment(4)
Well, that seems to have fixed it, thank you. Can you explain why?Sense
@TGP1994 :The AutoPostBack property is used to set or return whether or not an automatic post back occursAttired
When am selecting dropdown list , its going to first value of listCrinum
@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.Miley
G
83

try setting AutoPostBack="True" on the DropDownList.

Grease answered 5/2, 2011 at 6:5 Comment(0)
P
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

Pavo answered 7/3, 2012 at 14:22 Comment(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!Neoplasticism
the selected value of dropdown was having null in many items. please check in query that value is not null.Segarra
T
14

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

Timothea answered 10/10, 2013 at 10:41 Comment(1)
the ViewState properties are not needed, and the AutoPostBack was suggested in multiple answers years prior to this one.Trulatrull
E
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.

Enticement answered 24/10, 2016 at 12:4 Comment(3)
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.Godfree
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.Trulatrull
@Trulatrull 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.Enticement
G
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>
Gambrel answered 26/7, 2016 at 17:45 Comment(0)
N
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
}
Nailhead answered 2/7, 2013 at 5:30 Comment(1)
Only if the AutoEventWireup setting on the aspx page is true, otherwise you need to do it manually, as per the original example.Supplication
T
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.

Tate answered 27/7, 2023 at 6:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.