11 December 2006 - 11:43 PM / by Dominic Pettifer. 4 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...
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
BLACK UGG WOMENS HIGHKOO BOOTS 5765
[url=http://www.uggboots86.com/black-ugg-womens-highkoo-boots-5765-p-243.html]BLACK UGG [/url] [link=http://www.uggboots86.com]BLACK UGG [/link]
ESPRESSO UGG WOMENS HIGHKOO BOOTS 5765
[url=http://www.uggboots86.com/espresso-ugg-womens-highkoo-boots-5765-p-244.html]ESPRESSO UGG [/url] [link=http://www.uggboots86.com]ESPRESSO UGG [/link]
Posted on 5 September 2011 - 3:15 AM / by linlisa
ugg sale ugg sale ugg sale ugg sale
Posted on 1 October 2011 - 9:36 AM / by ugg sale
Search engine optimization (SEO Company USA) can be defined as an activity that undertakes web promotion of websites/portals and web pages. The quality SEO services ensure ranking of a website in popular search engines such as Yahoo, Google, Bing and MSN.
Posted on 4 January 2012 - 9:50 AM / by WEB Development India
http://www.jordanshoes2012onsale.com
jordan 1
Posted on 16 September 2011 - 9:43 AM / by Air Jordan 2
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 21 September 2011 - 9:50 AM / by cheap ugg boots
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 7 November 2011 - 3:58 AM / by Anonymous
Thigh subcutaneous <a href="http://www.myloveshoes.net">Christian Louboutin Shoes</a> fat accumulation is very easy to relax, and more sturdy than the knee, and thick thighs easy reason is that less developed <a href="http://www.myloveshoes.net/Categories_christian-louboutin-boots_5.html">Christian Louboutin Boots</a> thigh muscles, subcutaneous fat is too soft reasons for this.
Posted on 24 November 2011 - 2:31 AM / by cheap christian louboutins
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
[url=http://www.nikeam.com]Air shoes[/url]
[url=http://www.nikeam.com]Nike Air shoes[/url]
Posted on 30 May 2011 - 9:16 AM / by Air shoes
Newest styles of <a href="http://www.christianlouboutinreplicacl.com/"><strong>christian louboutin high heels</strong></a> in hot sale now, <a href="http://www.christianlouboutinreplicacl.com/"><strong>Christian Louboutin Knockoffs</strong></a> shoes sale now, buy <a href="http://www.christianlouboutinreplicacl.com/"><strong>christian louboutin replica</strong></a> in our online uk store .your shoes sales prices will save.
Posted on 22 September 2011 - 2:25 AM / by christian louboutin replica
<b><a href="http://www.ugg-bootuk.co.uk/">ugg sale uk</a></b> l <b><a href="http://www.uggs--australia.co.uk/">uggs australia</a></b> <b><a href="http://www.uggs--australia.co.uk/">ugg australia sale</a></b> last year , as <b><a href="http://www.ugg-for-sale.co.uk/">ugg for sale</a></b> long <b><a href="http://www.uggbootsaustralia.me.uk/">uggs for cheap on sale</a></b> as <b><a href="http://www.ugg-bootsonsale.co.uk/">ugg uk sale</a></b> the <b><a href="http://www.uggs--australia.co.uk/">ugg outlet store</a></b> residential <b><a href="http://www.ugg-bootsonsale.co.uk/">ugg boots on sale</a></b> one <b><a href="http://www.ugg-for-sale.co.uk/">ugg boot discount</a></b> was <b><a href="http://www.ugg-bootuk.co.uk/">ugg boot uk</a></b> <b><a href="http://www.uggbootsaustralia.me.uk/">shop ugg online</a></b> firecrackers , the <b><a href="http://www.ugg-bootsonsale.co.uk/">uggs uk cheap</a></b> child <b><a href="http://www.ugg-for-sale.co.uk/">ugg australia boots</a></b> began to cry . <b><a href="http://www.uggbootsaustralia.me.uk/">ugg boots australia</a></b> In despe... <b><a href="http://www.ugg-bootuk.co.uk/">cheap ugg boots uk</a></b>
Posted on 14 October 2011 - 8:38 AM / by ugg boot clearance
Bond reveals a more human side to his character. (from the blog It’s Bond, But Not as You Know it )
And YouTube still auto-fucking-plays videos!! This is TWO-THOUSAND-AND-FUCKING-TWELVE FFS!!!
about 20 hours ago from webOn a side-note, YouTube's commenting system is god-awful atrocious dreadful horrible horrible horrible!! Constant meaningless error messages
about 20 hours ago from webJavaScript is slow mmmkay http://t.co/NbB4eQjw - Actually, no, it's not http://t.co/kpGEIoPO #nodejs
about 20 hours ago from webTFS: It's super expensive, so it must be brilliant, right? Like Sharepoint #tekpubtfstitlesuggestion
5:22 PM February 3rd from web