Tuesday, October 17, 2006

RegisterClientScriptBlock isn't adding the script to the page

I have a bunch of ASP.NET custom controls that I've written to simpilfy some things that we do all the time (like disable a submit button after a click). To achieve a lot of this functionality, I've written some javascript and had them emitted via the Page.RegisterClientScriptBlock method.

When we converted these controls to ASP.NET 2.0, the script blocks no longer were being written to the page. Luckily I found a blog that mentions that the Register Script calls need to be done in the OnPreRender event because if you it do it in some other event, it will be too late.

Please note that the Page.RegisterClientScriptBlock has been deprecated and replaced by Page.ClientScript.RegisterClientScriptBlock (Thanks to Travis for reminding me to point this out).

Source: Page.RegisterClientScriptBlock isn't adding the script to the page...:

2 comments:

Anonymous said...

Don't use Page.RegisterClientScriptBlock. It has been depreciated. Instead use the ClientScriptManager class. You have a ton of new tools at your disposal. And as you can see in the example code, it works in Page_Load. :-)

Cody Schouten said...

Yeah, I forgot to mention that in the post. My main point was that the RegisterClientScript calls need to be done in the OnPreRender event.