Jackson Lopes
Jackson Lopes

Reputation: 255

dropdownlist onchange event not firing

I have a dropdownlist like below:

<asp:DropDownList ID="ddlExtraHoursStatus" runat="server" CssClass="dropdown" 
                                                Width="130px">
                                            </asp:DropDownList>

In my code what is do is I set its attribute on Page_load to call a JS function on its 'onchange' event. It works perfectly when I run the page and change the dropdown selections.

The problem or the question I have is why is the onchange event not fired(which calls the JS function) when I change the dropdown's selectedvalue property in code behind?????

I am stuck here, coz my further process depends on this. Please help

Upvotes: 0

Views: 2062

Answers (1)

Priyank
Priyank

Reputation: 1384

After the value of the dropdown is changed in the server side the page loads as fresh for the browser and javascript(in case you are not using update panel). So at the load of the page at client side you need to call the function which is being called at onchange event. you can do this as below (if you are using jquery)

 jQuery(document).ready(function (){
  DropdwonOnChangeFunction();
 });

If you want to call the function only if the value in the dropdown has changed, you need to maintain a hidden flag which has the previous selected value of the drop down. You can set this field at start of page load in code behind. And then using the value from this field you can decide if the function is to be called or not. As below.

  jQuery(document).ready(function (){
     if( $("#hdnDdlOldValue").val()!=$("#ddlExtraHoursStatus").val())
     {
      DropdwonOnChangeFunction();
      }
     });

Here i have used static ids. You can use the client ids.

Upvotes: 1

Related Questions