如何在Web用户Control ascx中调用javascript函数

时间:2014-06-05 13:44:06

标签: c# javascript asp.net

我在test.ascx ASP控制

上有以下代码
  function mute() {       
        var button_mute = document.getElementById('<%= btnRequestCompanyCheck.ClientID %>');
        button_mute.style.display = "none";
        alert("x");

    }

如何从Code后面调用mute()(test.ascx.cs),我正在尝试以下所有列表,没有人为我工作。 我应该在 Asp.net Control 上使用哪一个?

ScriptManager.RegisterOnSubmitStatement(this, this.GetType(), "test", "mute()");
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "test", "mute()", true);
ScriptManager.RegisterStartupScript(this, this.GetType(), "test", "mute()", true);
ScriptManager.RegisterStartupScript(this, this.GetType(), "test", "mute()", true);
Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction","mute()", true);

3 个答案:

答案 0 :(得分:3)

你试过这个吗?

<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="javascript:return mute();" />

这是javascript代码。

<script type="text/javascript">
function mute() {
    alert("Muted");

    return false;
}
</script>

以下是替代

背后的代码
Page.ClientScript.RegisterStartupScript(this.GetType(), "Script", "mute()", true);

答案 1 :(得分:0)

你必须在父控件上注册ScriptManager,如

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>

你可以使用后,

 Page.ClientScript.RegisterStartupScript(GetType(), "indexonchange", "OnIndexChange();", true);

答案 2 :(得分:0)

试试这个:

ScriptManager.RegisterStartupScript(this, this.GetType(), "test", "javascript:mute();", true);
相关问题