在DropDownList SelectedIndexChanged事件上调用JavaScript函数:

在DropDownList SelectedIndexChanged事件上调用JavaScript函数:,javascript,asp.net,visual-studio-2010,drop-down-menu,Javascript,Asp.net,Visual Studio 2010,Drop Down Menu,我编写了一个JavaScript函数,如下所示: function CalcTotalAmt() { ---------- ----------- } 我有一个下拉列表 <asp:DropDownList ID="ddl" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddl_SelectedIndexChanged"></asp:DropDownList> 但是Ja

我编写了一个JavaScript函数,如下所示:

 function CalcTotalAmt() 
 {
    ----------
    -----------
 }
我有一个下拉列表

  <asp:DropDownList ID="ddl" runat="server"  AutoPostBack="true" OnSelectedIndexChanged="ddl_SelectedIndexChanged"></asp:DropDownList>
但是JavaScript函数没有执行。 如何在DropDownList更改事件中调用JavaScript函数?

第一个方法:(已测试)

.aspx.cs中的代码:

 protected void Page_Load(object sender, EventArgs e)
    {
        ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
        if (!Page.IsPostBack)
        {
            ddl.Attributes.Add("onchange", "CalcTotalAmt();");
        }
    }

    protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
    {
       //Your Code
    }
protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Params["__EVENTARGUMENT"] != null && Request.Params["__EVENTARGUMENT"].Equals("ddlchange"))
                ddl_SelectedIndexChanged(sender, e);
            if (!Page.IsPostBack)
            {
                ddl.Attributes.Add("onchange", "CalcTotalAmt();");
            }
        }

        protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Your Code
        }
JavaScript函数:从JS函数返回true

   function CalcTotalAmt() 
 {
//Your Code
 }
function CalcTotalAmt() {
         //Your Code
     __doPostBack("ctl00$MainContent$ddl","ddlchange");
 }
.aspx代码:

<asp:DropDownList ID="ddl" runat="server"  AutoPostBack="true">
        <asp:ListItem Text="a" Value="a"></asp:ListItem>
         <asp:ListItem Text="b" Value="b"></asp:ListItem>
        </asp:DropDownList>
<asp:DropDownList ID="ddl" runat="server"  AutoPostBack="true">
        <asp:ListItem Text="a" Value="a"></asp:ListItem>
         <asp:ListItem Text="b" Value="b"></asp:ListItem>
        </asp:DropDownList>
JavaScript函数:从JS函数返回true

   function CalcTotalAmt() 
 {
//Your Code
 }
function CalcTotalAmt() {
         //Your Code
     __doPostBack("ctl00$MainContent$ddl","ddlchange");
 }
.aspx代码:

<asp:DropDownList ID="ddl" runat="server"  AutoPostBack="true">
        <asp:ListItem Text="a" Value="a"></asp:ListItem>
         <asp:ListItem Text="b" Value="b"></asp:ListItem>
        </asp:DropDownList>
<asp:DropDownList ID="ddl" runat="server"  AutoPostBack="true">
        <asp:ListItem Text="a" Value="a"></asp:ListItem>
         <asp:ListItem Text="b" Value="b"></asp:ListItem>
        </asp:DropDownList>

或者您也可以这样做:

<asp:DropDownList ID="ddl" runat="server"  AutoPostBack="true" onchange="javascript:CalcTotalAmt();" OnSelectedIndexChanged="ddl_SelectedIndexChanged"></asp:DropDownList>

您可以使用
ScriptManager.RegisterStartupScript()
从服务器调用任何javascript事件/客户端事件。例如,使用javascript的
alert()显示消息,您可以执行以下操作:

protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
Response.write("<script>alert('This is my message');</script>");
 //----or alternatively and to be more proper
 ScriptManager.RegisterStartupScript(this, this.GetType(), "callJSFunction", "alert('This is my message')", true);
}

Javascript函数正在执行。。但我在selectedindexchanged事件中编写了一些代码。但这一事件并没有发生。。如何解决这个问题?但是当我单击某个事件时,首先触发selectedIndexChanged事件,然后触发相应的事件。我不知道为什么会发生这种情况?如果您想在下拉列表的服务器端更改事件期间触发该事件,请尝试通过Page.RegisterStartUpScript方法注册脚本。我知道这是一个老问题,但您确定要在此处引发回发吗?如果服务器端代码所做的一切都是调用客户端代码,那么为什么不直接在客户端运行服务器呢?