Rendering Client Script in ASP.NET 2.0 : Behavior changes

In ASP.NET 2.0, all client script rendering functions are grouped under the ClientScriptManager class, which is availabe through Page.ClientScript property. The older script related methods on the Page class (RegisterClientScript, RegisterStartupScript etc.) are still available, but deprecated. In addition, most Script related methods now require an additional paramater: Type or Control. This helps isolate scripts generated by different types of WebControls, and would be more useful to Control developers.

There are also changes in how the script are rendered. For example, the startup script is now rendered right before the FORM (runat='server') is closed, not before the closing of the BODY element. If your Form element is wrapping everything else in the Body element, its not a problem, but if your Form is nested within other elements (within some DIV elements, for example) it may result in unexpected behavior. For example, the DOM structure may not be ready when the Startup script is executed.

Similarly, RegisterClientScript method adds scripts in the beginning of the FORM element, rather than top of the Body. And here is the biggest problem:

No FORM, No Client Script!

Thats right, if there is no Form with runat='server' on your page, these ClientScript methods will not render any script. You won't get any error message, but no script will be rendered on the Page. ASP.NET 2.0 expects that every Web page includes a server side FORM element.

To most developers used to the ways of ASP.NET, this may not be a problem. However, for Control developers, this may be a potential problem. If your Controls depend heavily on Client scripts, be aware of this behavior and possibility of ASP.NET pages not including a server side Form tag. 

It may be a good idea to include a server side Form tag on  every ASP.NET page, just to make sure all containing Controls, specially third party Controls  would function as expected.

Posted on September 25, 2007 19:46 by Haider

Comments

October 4. 2007 08:56

Pallab

I have used this Page.ClientScript property several times in my codes. However I didn't know about the problem it might cause while using outside the form.

Thanks for your information. Smile

Pallab

December 4. 2007 02:20

Vybber

Thanks a lot!!! I spent 8 hours for this dame thing and very angry on Microsoft - them don`t added this very usefull remarks to MSDN Smile

Vybber

Don't Post SPAM

If you are posting a commment just to get a link, don't waste your time!

I have a sophisticated comment moderation system in place, and your comment will not be posted.

Add comment




biuquote
  • Comment
  • Preview
Loading