Friday, February 29, 2008

Wednesday, February 27, 2008

Dynamic Rendering of META Description HTML tag

For today's search engine optimized web sites having relevant content of the META Description HTML tag on a page is a must. It plays a significant role together with the page title in determining a page relevance ranking in regards to search keywords.

So it becomes necessary to generate a content of the META Description dynamically based on a page content. ASP.NET already provides an easy access to the page TITLE tag allowing to populate it with relevant text simply using a Page.Title property.

Unfortunately it's not the same easy and obvious how to access the META Description from the server-side code.

Luckily ASP.NET is flexible enough and the solution is easy. Simply add a META tag to your page and turn it into a server tag by adding an id and a runat="server" attributes like the following:

<meta name="description" content="your description" runat="server" id="Description" />

Immediately you'll be able to access the content from the code behind by using a Description.Content property.

The explanation is very simple, actually. When ASP.NET parses the mark-up it creates an HtmlMeta server control with the name "Description" that has a Content property of a string type. And, of course, you can employ the same technique if you want to manipulate with other META tags on the page. Just use the proper value of a "name" attribute.