+5  A: 

There are a number of ways to do this, more than likely the "right" way of doing this would be to build out the string, then use the Page.ClientScript.RegisterClientScriptBlock method to register the JS, this will put it in the right place

Another alternative, that is more of the quick and dirty way would be to add an control to the page, and rather than response.write, build out the JS, then set the literal control's text property.

Mitchel Sellers
+1 - we posted *the same second*, but your answer is better :)
Fredrik Mörk
Thanks, I actually re-worded it as well, as technically Page.ClientScript is the "better" and right way to do it...
Mitchel Sellers
A: 

Put the generated JavaScript code into a property on the master-page, and the master-page handle it. The master-page renders after the pages it encapsulates, but it's members are available to it's content-pages.

roosteronacid
+1  A: 

The easiest way to replace the Response.Write calls would probably be to use a StringBuilder:

StringBuilder sb = new StringBuilder();
sb.Append("your script writes here"); // instead of Response.Write

And use the Page.ClientScript manager to put the script where it could be used. There are several methods to register the script, and each one places the script in different locations on the page, depending on the lifecycle of the current control you are in. Here is one example (already covered above):

Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "yourScriptKey", sb.ToString());
FlashXSFX