Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an asp.net button, that when clicked calls a code behind function. The function does some evaluation, and then I want to call javascript from within this asp.net function.

share|improve this question
add comment

2 Answers

up vote 5 down vote accepted

While pranay's answer is correct in the function call, the rest is off, here's hopefully a better explanation:

You can use ClientScript.RegisterStartupScript() to do what you want, like this:

void myButton_Click(object sender, EventArgs e)
{
  var script = "alert('hi');";    
  ClientScript.RegisterStartupScript(typeof(Page), "ButtonAlert", script, true);
}

The format is (type, scriptKey, scriptText, wrapItInScriptTags).

If you are operating with an UpdatePanel, it's very similar, except you need to use the slightly different ScriptManager.ResgisterStartupScript(). Use the UpdatePanel as the control and type parameters in that case to be safe.

share|improve this answer
add comment

JavaScript is a client side technology.

While I suppose it would be theoretically possible to run the code on the server I don't see what benefit it would give you.

What do you actually want to achieve?

share|improve this answer
 
I have an iFrame with an asp.net page inside it. The URL that sets the source for the iFrame contains a querystring parameter with a return URL as the value. When the code in the asp.net page in the iFrame is processed (when the submit button is clicked), it runs some server side code, which if returns 'true' will redirect the parent page of the iFrame upon success. –  Dkong May 25 '10 at 13:30
add comment

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.