Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call JavaScript function on DropDownList SelectedIndexChanged Event:

I have written one JavaScript function as follows:

 function CalcTotalAmt() 
 {
    ----------
    -----------
 }

I have one DropDownList,

  <asp:DropDownList ID="ddl" runat="server"  AutoPostBack="true" OnSelectedIndexChanged="ddl_SelectedIndexChanged"></asp:DropDownList>

I need to call the above JavaScript function in the DropDownList's SelectedIndexChanged Event. I tried like below;

protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
    ddl.Attributes.Add("onchange", "return CalcTotalAmt();");
}

But the JavaScript function is not executing. How to call the JavaScript function in DropDownList Change Event?

like image 760
thevan Avatar asked Jun 02 '12 11:06

thevan


People also ask

How to call JavaScript function in DropDownList SelectedIndexChanged?

First the ASP.Net DropDownList will be referenced using JavaScript or jQuery on Client Side and then its SelectedIndexChanged event will be called by making use of the __doPostBack JavaScript function.

How to call JavaScript function on DropDown selected index change in mvc?

Add("onChange", "return OnSelectedIndexChange();") <code> OnSelectedIndexChange()</code> should be your Javascript function that will be call <code>DropDownList </code> Seleted index is changes. You can also pass the object to get the value of selected item as well.

What is ASP DropDownList?

The DropDownList control is a web server element that creates the drop-down menu and lets users select a single item from various options. You can add any number of items to the DropDownList. Almost all web pages contain a drop-down list, but it is mostly present in registration forms or sign-in pages.


1 Answers

Or you can do it like as well:

<asp:DropDownList ID="ddl" runat="server"  AutoPostBack="true" onchange="javascript:CalcTotalAmt();" OnSelectedIndexChanged="ddl_SelectedIndexChanged"></asp:DropDownList>
like image 67
Imran Balouch Avatar answered Sep 21 '22 19:09

Imran Balouch