11 December 2006 - 11:43 PM / by Dominic Pettifer. 5 Comments for Get a<HEAD> with ASP.NET 2.0.
Technical Article - New in ASP.NET 2.0 is the ability to add runat="server" to the pages <head> tag. This lets you programmatically modify the head element and other elements inside it such as, Title, adding JavaScript code and style attributes, plus many more. Here I show how this is done.
Link tags allow us to add links to external resources, such as CSS files, JavaScript files, RSS feeds and so on. This is useful in that the browser will cache these external resources, instead of downloading them again if they were embedded amongst the HTML mark-up or in the head tag. This will help cut down on bandwidth and make pages download quicker.
HtmlLink cssLink = new HtmlLink();
cssLink.Href = "styles.css";
cssLink.Attributes.Add("rel", "Stylesheet");
cssLink.Attributes.Add("type", "text/css");
this.Header.Controls.Add(cssLink);This will add the following link to the head...
<link rel="Stylesheet" href="styles.css" type="text/css" />
Note: Funny how there's no type safety via properties for the 'rel' and 'type' attributes.
Finally we can also add JavaScript code to our page. This is done pretty much the same way as style elements...
HtmlGenericControl javaScript = new HtmlGenericControl("script");
javaScript.Attributes.Add("type", "text/javascript");
javaScript.InnerText = "alert('Hello World!');";
this.Header.Controls.Add(javaScript);Although there is perhaps a slightly better way to do it via the ClientScript property that exposes a ClientScriptManager object. With this object we can use the RegisterClientScriptBlock() method to dynamically render a JavaScript block just after the start of the form element (we need a form element with runat="server" somewhere in the page for this to work)...
this.ClientScript.RegisterClientScriptBlock(this.GetType(),
"Script Name", "alert('Hello World!');", true);The second argument 'Script Name' is a unique name for the script block. If we repeat this code again we'll still end up with one script block, however if we change the name to 'Script Name 2' for the second line we'll have two script blocks.
This is useful for custom user/server controls that rely on JavaScript for their implementation, as it stops multiple JavaScript blocks being added if the same control is used multiple times in a page (see upcoming blog on custom server controls).
The other approach is to use the RegisterStartupScript() method. This behaves exactly the same as the RegisterClientScriptBlock() method, except with RegisterStartupScript() the JavaScript block is rendered just before the final </form> closing tag.
As its name suggests, we can run startup scripts in items inside the form element of our page (or even items before then), because the script block is rendered last, and other elements before it have already been rendered in the page flow ready to be manipulated by the script block (via CSS/DOM).
Thanks for reading and hope you have found this article useful. I don't claim to be some expert guru or anything, so I have probably missed out something important here, or got something completely wrong. If I have then please drop me a line, or leave a comment. Thank you!
Previous PageJump to Page...
One acknowledges that our life seems to be very expensive, however different people require cash for various stuff and not every one gets enough cash. Thence to get quick <a href="http://bestfinance-blog.com">loans</a> and just short term loan will be a right solution.
Posted on 5 September 2010 - 2:10 AM / by WALKERJULIA26
Thanks, Dude!
I needed to change content page styles based on user actions in my master page, which process AFTER the content loads. I was able to use your examples and point my content controls to styles in the master page and it worked!!
Posted on 7 January 2010 - 10:30 PM / by Amber
Thank you very much.
After converting <head> to server control stylesheet stopped working because of inline variable used for href.
Your article helpled to solve the problem by converting the style tag to server control and then adding 'href' attribute from code behind.
Posted on 5 January 2009 - 3:59 PM / by chandra
Thanks man,
couldn't find nowhere how to insert a script tag!
Posted on 23 August 2007 - 2:56 PM / by eddy
You could most of this with .net 1
i would give the header an id and runat server and i could add <script> on the fly.
Posted on 12 June 2007 - 2:05 PM / by comet
Result of Stored Procedure using output params and a recordset (from the blog Output Parameters AND Recordsets From a Stored Procedure )
@gafroninja Does your hair stand up for 10 mins each day, then you have a Scrum haircut
1:37 PM September 3rd from Echofon@scottgal Teeth are nature's nail clippers :-)
10:42 AM September 3rd from Echofon@brucel Those are amazing! Does the 6th one down (psychedelic colours) mean we can create gradient effects in CSS3? #css3
4:28 PM September 2nd from EchofonSorry for the rant, but if you tweet YouTube videos hidden behind URL shortening services, please warn beforehand
2:01 PM September 2nd from EchofonWHOEVER THOUGHT AUTO PLAYING VIDEOS WERE A GOOD IDEA SHOULD BE SHOT!!!!!!!1!!!!11!1!!
1:57 PM September 2nd from Echofon