ClientScriptManager…why didn’t that work

NET 2.0 added the ClientScriptManager to allow developers a simple method to add javascript and other "Script" capabilites to a web page easily.  However, the documentation never really got developed, so attempting to use it can be a bit trick.

 
Lets start with adding an include of a .js file.

protected

override void Render(HtmlTextWriter writer)
{
if (!this.Page.ClientScript.IsClientScriptIncludeRegistered(this.GetType(), VirtualEarthSettings
.VirtualEarthJScriptTagHeader))
{
this.Page.ClientScript.RegisterClientScriptInclude(this.GetType(), VirtualEarthSettings.VirtualEarthJScriptTagHeader, http://dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6
);
}
base
.Render(writer);
}

Didn’t work did it? 

Why?
You can’t call RegisterClientScriptInclude(…) after the PreRender section of the page lifecycle.  Best bet: call in OnInit.
 
In fact about the ONLY ClientScriptManager function you can use after the PreRender is: ClientScript.RegisterStartupScript
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s