Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
add comment (requires an account with 50 reputation)

4 Answers

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);
share|improve this answer
add comment (requires an account with 50 reputation)

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

share|improve this answer
add comment (requires an account with 50 reputation)

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.

share|improve this answer
add comment (requires an account with 50 reputation)

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.

share|improve this answer
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.