Tutorial: How to add specific page keywords & descriptions while using ASP.NET Master Pages

One of the most common questions that gets asked when starting out with ASP.NET Master Pages is how to add page specific keywords & descriptions.

For those not familiar with ASP.NET and/or Master Pages is that while using Master Pages the normal HTML tags including META tags are not part of individual pages. The individual ASP.NET pages only refer to content areas called, content place holders. The advantage of using Master Pages is that your site HTML template gets referenced in one area so you are able to make site wide changes to the Master Page instead of making the same changes to every individual web page within the site.

So to add either keywords or a description to your individual page you will need to create an HtmlMeta object as show below.


/* how to code */
protected void Page_Load(object sender, EventArgs e)
{
// variables
String sKeywords = String.Empty;
String sDescription = String.Empty;

// page keywords
sKeywords = “Place your page keywords and phrases here.”;

// page description
sDescription = “Place your page description here.”;

// meta tag keywords
HtmlMeta mKeywords = new HtmlMeta();
mKeywords.Name = “keywords”;
mKeywords.Content = sKeywords;
Header.Controls.Add(mKeywords);

// meta tag description
HtmlMeta mDescription = new HtmlMeta();
mDescription.Name = “description”;
mDescription.Content = sDescription;
Header.Controls.Add(mDescription);
}

Categories

Archives