11 December 2006 - 11:43 PM / by Dominic Pettifer. 7 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.
These techniques are useful in the ASP.NET programmer's arsenal because dynamically setting values in the pages head tag is very common. Before, ASP.NET developers had to rely on inline variables inside the page's head tag, or, heaven forbid, insert inline C#/VB code into the html page itself.
Now with ASP.NET 2.0 the runat="server" attribute allows you to programmatically set header values in the aspx pages' code behind, and keep absolutely all code out of the html itself. In the aspx/html page add the runat attribute to the head tag like so...
<head runat="server">
<title>Page Title Here</title>
</head>In the code behind you gain access to the header via the Page classes Header property. This Header property exposes a whole number of other properties and methods itself, some we'll learn about here...
The most common need for programmatic header manipulation is to set the pages <title> tag dynamically. This is useful for Search Engine Optimisation because the title becomes the clickable link the user sees when your sites pages come up in a search. Also search engines pay special attention in particular to the Title tag when indexing pages.
this.Header.Title = "Page Title Here";
For search engine optimisation purpose it's useful to have a highly descriptive title for your page.
Meta tags are useful for Meta data about your pages such as a description, keywords and author. They are usually used by search engines to index your page. Often you want these rendered dynamically dependant on content being pulled from a database for instance. Take this page, if you right click and view source you'll see description and keywords related to this particular blog.
Rather than statically create the meta tags and add the content dynamically, we'll dynamically create the meta tags themselves. For this we'll create an HtmlMeta control, set it's name and content, and add it to the Header properties Controls Collection, like so...
HtmlMeta metaDescription = new HtmlMeta(); metaDescription.Name = "description"; metaDescription.Content = "A description of the page here."; // Add to the header’s controls collection this.Header.Controls.Add(metaDescription);
This will get rendered in the head as...
<meta name="description" content="A description of the page here." />
We can add keywords in the same way, just set the Name property to 'keywords' and Content accordingly. Don't fret about keywords too much. Some search engines, such as Google, ignore them due to the keywords meta tag frequently being spammed by websites trying to increase their page rank.
Search Engine Optimisation is beyond the scope of this article, but there are plenty of resources on it.
Sometimes, though not often, we may want to dynamically add or manipulate a page level style sheet. For instance, if we have a complex custom user control with various CSS style attributes applied, we could instead place the CSS in a style element inside the head, instead of having them inline amongst the HTML. Although for some reason the built in controls (such as the Calendar control) uses inline styles, so maybe I'm missing something here?
In any case, the style element doesn't directly map to any built in ASP control, but we can use an HtmlGenericControl instead, and set the tag name, attributes and content manually, like so...
HtmlGenericControl styles = new HtmlGenericControl("style");
styles.Attributes.Add("type", "text/css");
styles.InnerText = "p { font-weight: bold; }";
this.Header.Controls.Add(styles);This will result in the following mark-up in your head tag...
<style type="text/css"> p { font-weight: bold; } </style>Bear in mind it's possible to add multiple style elements to the head tag, if this code was repeated (making sure to create a new object reference), one wouldn't override the other, you'd just have two identical style elements in the head.
An alternative is to add the runat="server" and id attributes to a hand written style element already inside the head tag like so...
<style type=”text/css” runat=”server” id=”pageStyles”>
This then becomes a variable that we can access it from the code behind and add additional styles to if needed, like so...
pageStyles.InnerText += "h1 { font-size: 150%; }";Although there is no type safety with the CSS styles themselves, we're basically adding and removing text content, so it would be difficult to programmatically remove individual CSS elements, unless we resorted to complex string operations.
Next PageJump to Page...
I don't know what happened, but, costs for custom term papers seem to be reduced. Probably, financial state of the market has something to deal with it? Really, I don't care about it. I'm glad that I have a chance to ask: " do my paper ".
Posted on 6 September 2010 - 11:41 AM / by buying term paper
It would be interesting to perform the college paper writing but it demands lots of efforts at any rate. Thence different students, which are lack of time, buy term papers online. In such way, they get a success. Thence why must you spend your free time?
Posted on 6 September 2010 - 11:09 AM / by buy customized reports and essays
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
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!!
@andrewmy I'm thinking of using Git myself, what Windows client do you use? Is TortoiseGit the defacto standard?
about 5 hours ago from EchofonThe new Google Logo is awesome, almost as good as Pacman
about 8 hours ago from Echofon"Normalization is from the devil" - do you agree? http://ayende.com/Blog/archive/2010/09/06/normalization-is-from-the-devil.aspx
12:48 PM September 6th from Echofon@ellieemptylemon Thanks for those, although company I work for is looking to send us on training courses.
11:19 AM September 6th from Echofon