ASP.NET 2.0: Place JavaScript inside the Page.Header

The Page.ClientScript object allows you to place JavaScript inside the Page, but it’s limited in the fact that you cannot use it to place JavaScript inside the Head tags of the Page. But fortunately there is a way to do this.

Add a .js file include inside the page header:

 HtmlGenericControl Include = new HtmlGenericControl("script");
 Include.Attributes.Add("type", "text/javascript");
 Include.Attributes.Add("src", "http://dev.virtualearth.net/mapcontrol/v3/mapcontrol.js");
 this.Page.Header.Controls.Add(Include);

Add some JavaScript inside the page header:

 HtmlGenericControl Include2 = new HtmlGenericControl("script");
 Include2.Attributes.Add("type", "text/javascript");
 Include2.InnerHtml = "alert('JavaScript in Page Header');";
 this.Page.Header.Controls.Add(Include2);

It would be nice if there was a Page.Header.ClientScript object that you could use to place JavaScript within the header of the Page, but for now we’ll just have to use the method stated above.