1

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.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);
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. Commented Jun 5, 2014 at 13:57
  • @Tetsujion .. I used ImageButton_OnClickEvent to call mute() javascript Commented Jun 5, 2014 at 14:14
  • @TetsujinnoOni .. so when the user Click on ImageButton i hide the Image button and add new text .. Commented Jun 5, 2014 at 14:40
  • 1
    possible duplicate of Calling a javascript method from the code behind Commented Jun 5, 2014 at 16:15

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

8 Comments

thanks ..i trying to do that before .. it is working ..but i have some validation from code behind ,so i need to call script function from code behind ..
Page.ClientScript.RegisterStartupScript(this.GetType(), "Script", "mute()", true);...Not working ..:(.. i used this code inside ImageButton_onClick() event ..
Not working means? Are you not any responses / any script errors?
no Response.. and no java SCript Error .. i used WebDevloper-->webConsole in FireFox
can view source and did you see something like this? <script type="text/javascript"> //<![CDATA[ mute()//]]> </script>
|
0

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

1 Comment

could you please add how to call it from code behind ?
0

try this:

ScriptManager.RegisterStartupScript(this, this.GetType(), "test", "javascript:mute();", true);

3 Comments

javascript:mute(); try without ";"
try remove all the code from the mute() function and test it with only alert message to know if it works or not
no problem in mute() function ..mute running correctly from onClientClick event .. but i am trying to call it from code behind ..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.