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?
-
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.marc_s– marc_s2009-09-08 06:58:09 +00:00Commented Sep 8, 2009 at 6:58
-
5Please 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.David Andres– David Andres2009-09-08 06:59:15 +00:00Commented Sep 8, 2009 at 6:59
3 Answers
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.
Comments
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 !!!