0

Is there a way in web forms to call a javasctipt function after making a server call for example

function showAlert(){ 
   alert("hello");
}


 <asp:Button ID="callJavasctips" runat="server" Text="callJavasctipt" OnClick="callJavaSctipt_click" />  

In MVC I can say OnSuccess = "showAlert()" is there a way to do this in webforms?

UPDATE

I ended up using ScriptManager instead of Page.ClientScript because it didn't work with update panels.

2 Answers 2

1

For calling javascript after, you'll have to use ClientScriptManager.RegisterStartupScript() on your button's click handler.

I'm not sure if you would be interested in this, but you can define onclientclick="some_javascript_snippet" for <asp:Button /> but it's called before the server side handler.

Sign up to request clarification or add additional context in comments.

Comments

0

In the callJavaSctipt_click event in the code behind, do the following:

Page.RegisterClientScriptBlock("MyScript","<SCRIPT Language='JavaScript'> alert('Hello'); </SCRIPT>");

4 Comments

Page.RegisterClientScriptBlock() is obsolete. see msdn msdn.microsoft.com/en-us/library/…
@crocaduck81. The difference between RegisterClientScriptBlock and RegisterStartupScript is that RegisterStartupScript adds the script to the top of the aspx page where as RegisterClientScriptBlock adds the script to the end. The choice is important if you are accessing any html controls from the script. In this scenario it doesn't matter which ever you call.
@Bala R. Obsolete in the .NET 4.0 framework. But it could be used with framework earlier than that.
it's marked obsolete in .net 4 but it has been deprecated since .net 2.0, I think.

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.