BMMV
BMMV

Reputation: 53

Call Javascript from code behind function in asp.net

How to Call Javascript function from code behind after button click event;

 string popupScript = "<script language='javascript'>" +
                         "alert('hai');" +
                       "</script>";

                ClientScript.RegisterStartupScript(Page.GetType(), "script", popupScript, true);

I tried above script but not working

Upvotes: 1

Views: 8707

Answers (4)

rtharris619
rtharris619

Reputation: 46

Since ClientScript.RegisterStartupScript is an overloaded function you can also leave it without the last 'addScriptTags' boolean when you want to put in the script tags yourself. It defaults to false.

Upvotes: 1

Jonathan Henson
Jonathan Henson

Reputation: 8206

In addition to all of the advice of the others, if the element firing this event is in an update panel you will need a full post-back trigger.

Upvotes: 0

Daniel A. White
Daniel A. White

Reputation: 191037

Remove the language attribute. Change it to type='text/javascript'

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1039398

You already added the script tags, so pass false as last argument:

string popupScript = 
    "<script type=\"text/javascript\">" +
    "alert('hai');" +
    "</script>";
ClientScript.RegisterStartupScript(Page.GetType(), "script", popupScript, false);

or leave it to the framework:

string popupScript = "alert('hai');";
ClientScript.RegisterStartupScript(Page.GetType(), "script", popupScript, true);

Upvotes: 6

Related Questions