Changing Page Title and Meta Tags with Master Pages

In ASP.NET, Master Pages are good for so many things.

They also cause some confusion on how to have a different page title for each page, how to specify Meta Tags, etc.
This post is neither on Master Pages nor on best practices on handling these two topics. I would try to list couple of simple ways to handle them.
Title:
Title tag in Master Page acts as a place holder. On each page that uses Master Page, specify the “Title” in the page declarative.
In your content page, i.e., (.aspx),
<%@ Page Language=”C#” MasterPageFile=”~/Master1.master” AutoEventWireup=”true” Title=”Content Page Title” %>
You could also change the “title” tag in the code file as follows.
base.Master.Page.Header.Title = “Content Page Title”;

Meta Tags:
Meta Tags are little different compared to setting the page Title. Check the code that creates a meta tag “keywords” with content “ASP.NET, AJAX, Web Services”.
HtmlMeta metaTag = new HtmlMeta();
metaTag.Name = “keywords”;
metaTag.Content = “ASP.NET, AJAX, Web Services”;
base.Master.Page.Header.Controls.Add(metaTag);
If you need to do this in multiple content pages, you might want to have a base page with properties like, “MetaKeywords”, “MetaDescription”, “MetaRobots” etc. So, on your regular page you could set these properties.


No comments:

Post a Comment