user1511953
user1511953

Reputation: 43

dropdown list event is not firing when javascript is used

I have a problem in firing a dropdownlist event.

I have the following dropdown:

asp:dropdownlist id="ddlhello" Runat="server" AutoPostBack="True"  onchange="javascript:return ChangeHeader();"

I have also added an event in the code behind i.e. selectedindex change event.

Now the problem occurs when I execute the page: it executes javascript but the server side code does not fire. If I remove this line onchange="javascript:return ChangeHeader();" then the server side code fires.

When I checked the source page it shows me two onchange events associated: one for javascript and other for server side.

I think that it is picking client side code and neglects server side.

I am not pretty sure, so I want to know the behavior reason.

And what is the way out for this.

I want server and client side code to be executed. I have searched for solutions but I have not found any correct reason.

Please help me.

Upvotes: 0

Views: 1486

Answers (2)

Adil
Adil

Reputation: 148150

You can not bind the server side event with dropdown, bind it with onselectedindexchanged

onselectedindexchanged="DropDownList1_SelectedIndexChanged"

<asp:dropdownlist id="ddlhello" Runat="server" AutoPostBack="True"   onselectedindexchanged="DropDownList1_SelectedIndexChanged" onchange="javascript:return ChangeHeader();" </asp:dropdownlist>

Returning false from client stops the postback.

function ChangeHeader()
{
 //return false; // will stop the postback
  return true; //will cause postback
}

Upvotes: 1

Narendra Patel
Narendra Patel

Reputation: 317

Use like following instead of onchange="return ChangeHeader();" onchange="ChangeHeader();"

I hope this can help you.

Upvotes: 0

Related Questions