Kirk Evans Blog

.NET From a Markup Perspective

Dynamically Adding Meta Tags in ASP.NET 2.0

Dynamically Adding Meta Tags in ASP.NET 2.0

  • Comments 1

A customer asked how to dynamically add meta tags in ASP.NET 2.0 (for more information on HTML meta tags, see W3Schools). I opened up Visual Studio, and started looking for ways to manipulate the header using a clever runat="server" trick with a System.Web.UI.HtmlGenericControl.

It was apparent after just a moment of trying to do this that there had to be an easier way. All it took was going into the IDE and typing "Page." inside the Page_Load handler to inspect the members of the Page class. Yep, there it was… a member called "Header". Here I was about to start using a recursive method to compare types (FindControl uses a control's ID property, and by default the Head element in an ASPX page's markup has no ID).

Once I found the Header property and I saw that it, in turn, had a Controls property… it was then simple to determine if my life had been made even easier with a pre-built class that represents an HTML META tag. Yep… there it is, the System.Web.UI.HtmlControls.HtmlMeta class.

The end result ends up so much cleaner than what I originally started doing:


using System;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;



public partial class _Default : Page


protected void Page_Load(object sender, EventArgs e)


HtmlMeta keywords = new HtmlMeta();

keywords.Name = "keywords";

keywords.Content = "hockey, Thrashers, Atlanta, Phillips";




  • Year ago I have developed special control that do something like this. It has predefined categories of the metatags (this is for design time support) and supports databainding (this is useful forexample on dynamical sites where description, keywords, authors of the site is goes from the database)

    I someone iterested, it located at, of course it published with sources;

Page 1 of 1 (1 items)
Leave a Comment
  • Please add 4 and 4 and type the answer here:
  • Post
Translate This Page