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?
You can use the
ScriptManager.RegisterStartupScript();
to call any of your javascript event/Client Event from the server. For example, to display a message using javascript'salert();
, you can do this:To be exact for you, do this...
Or you can do it like as well:
First Method: (Tested)
Code in .aspx.cs:
JavaScript function: return true from your JS function
.aspx code:
Second Method: (Tested)
Code in .aspx.cs:
JavaScript function: return true from your JS function
.aspx code: