最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - how to call javascript function in Web user Control ascx - Stack Overflow

programmeradmin3浏览0评论

I have the following Code on test.ascx ASP Control:

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

    }

How I can call mute() from Code behind (test.ascx.cs), I am trying all of below list, no one is working for me. Which one should I used on Asp 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);

I have the following Code on test.ascx ASP Control:

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

    }

How I can call mute() from Code behind (test.ascx.cs), I am trying all of below list, no one is working for me. Which one should I used on Asp 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);
Share edited Aug 19, 2017 at 12:09 Mohamad Mahmoud Darwish asked Jun 5, 2014 at 13:44 Mohamad Mahmoud DarwishMohamad Mahmoud Darwish 4,1819 gold badges55 silver badges78 bronze badges 4
  • 2 mute() is supposed to occur in the browser in response to an event handler on the server? how is the server supposed to send that message to the client? is the event handler running as a full postback or partial postback? We need more info to help. – Tetsujin no Oni Commented Jun 5, 2014 at 13:57
  • @Tetsujion .. I used ImageButton_OnClickEvent to call mute() javascript – Mohamad Mahmoud Darwish Commented Jun 5, 2014 at 14:14
  • @TetsujinnoOni .. so when the user Click on ImageButton i hide the Image button and add new text .. – Mohamad Mahmoud Darwish Commented Jun 5, 2014 at 14:40
  • 1 possible duplicate of Calling a javascript method from the code behind – Tetsujin no Oni Commented Jun 5, 2014 at 16:15
Add a ment  | 

3 Answers 3

Reset to default 2

Did you tried this?

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

And here is the javascript code.

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

    return false;
}
</script>

Here is the code behind alternative

Page.ClientScript.RegisterStartupScript(this.GetType(), "Script", "mute()", true);

you have to register ScriptManager on parent control like

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

after you can use,

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

try this:

ScriptManager.RegisterStartupScript(this, this.GetType(), "test", "javascript:mute();", true);
发布评论

评论列表(0)

  1. 暂无评论