0

I have a javascript function called ShowConfirmationBox(). I want to call it in my C# code behind file. please anyone tell me how can i call it?

2
  • Your C# code-behind file is run on the server - you cannot call a client-side Javascript function on the server, really. You can add it to a button click or something - but it'll be called on the CLIENT in the browser. Commented Sep 8, 2009 at 6:58
  • 5
    Please note that all of the solutions provided here don't actually call ShowConfirmationBox from C# code. Some of the solutions will make sure the ShowConfirmationBox occurs when the page loads, while others call this function as a result of a button click. It is not possible to call client-side code from C# server-side code. Commented Sep 8, 2009 at 6:59

3 Answers 3

2

Simply use that to include your script into page :

string script = "ShowConfirmationBox();";

if (!this.Page.ClientScript.IsClientScriptBlockRegistered("myClientScript"))
{
    this.Page.ClientScript.RegisterClientScriptBlock(typeof(MyPage), "myClientScript", script);
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can use

Page.ClientScript.RegisterStartupScript() Method

public void RegisterStartupScript(
    Type type,
    string key,
    string script
)

Parameters

type Type: System..::.Type The type of the startup script to register.

key Type: System..::.String The key of the startup script to register.

script Type: System..::.String The startup script literal to register.

Reference

Comments

0

the comments to your question make more sense than the answers.

as was pointed out, you cannot call your javascript function from the server.

the solution is to figure out WHEN do you want to call your function.

if you want to call if on your ButtonClick event, there is OnClientClink even for the ASP:Button control. There are client-side events for all ASP.NET controls, use them !!!

Comments

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.