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

I have the following Code on test.ascx ASP Control:function mute() { var button_mute = document.g

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);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744816144a4595338.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信