Chris Pietschmann

husband, father, hacker, entrepreneur, futurist, innovator, autodidact


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", "");

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');";

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.

blog comments powered by Disqus