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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10861895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:17:05  来源:igfitidea点击:

Call JavaScript function on DropDownList SelectedIndexChanged Event:

javascriptasp.netvisual-studio-2010drop-down-menu

提问by thevan

I have written one JavaScript function as follows:

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

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

I have one DropDownList,

我有一个 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;

我需要在 DropDownList 的 SelectedIndexChanged 事件中调用上述 JavaScript 函数。我试过如下;

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?

但是 JavaScript 函数没有执行。如何在 DropDownList Change Event 中调用 JavaScript 函数?

采纳答案by Kapil Khandelwal

First Method: (Tested)

第一种方法:(已测试)

Code in .aspx.cs:

.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
    }

JavaScript function: return true from your JS function

JavaScript 函数:从你的 JS 函数返回 true

   function CalcTotalAmt() 
 {
//Your Code
 }

.aspx code:

.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>

Second Method: (Tested)

第二种方法:(已测试)

Code in .aspx.cs:

.aspx.cs 中的代码:

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 function: return true from your JS function

JavaScript 函数:从你的 JS 函数返回 true

function CalcTotalAmt() {
         //Your Code
     __doPostBack("ctl00$MainContent$ddl","ddlchange");
 }

.aspx code:

.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>

回答by Imran Balouch

Or you can do it like as well:

或者你也可以这样做:

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

回答by Cyberpks

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's alert();, you can do this:

您可以使用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);
}

To be exact for you, do this...

准确地说,请执行此操作...

protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
 ScriptManager.RegisterStartupScript(this, this.GetType(), "callJSFunction", "CalcTotalAmt();", true);
}