SelectedIndexChanged事件上调用JavaScr

SelectedIndexChanged事件上调用JavaScr

本文介绍了在DropDownList SelectedIndexChanged事件上调用JavaScript函数:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have written one JavaScript function as follows:

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

我有一个DropDownList,

I have one DropDownList,

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

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

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();");
}

但是JavaScript函数未执行。
如何在DropDownList更改事件中调用JavaScript函数?

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

推荐答案

第一种方法:(已测试)

.aspx.cs中的代码:

Code in .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函数:从JS函数返回true

JavaScript function: return true from your JS function

   function CalcTotalAmt()
 {
//Your 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>

第二种方法:(已测试)

.aspx.cs中的代码:

Code in .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函数:从JS函数返回true

JavaScript function: return true from your JS function

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>

这篇关于在DropDownList SelectedIndexChanged事件上调用JavaScript函数:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 01:28