Changing Page Title and Meta Tags with Master Pages

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.

5 thoughts on “Changing Page Title and Meta Tags with Master Pages

  1. Hi,

    I am doing this from so many days actually what i am doing on click of menu it opens up new ShowModalDialog and in it i pass some value in query string and based on it Page.Title property i set by code on first page of wizard[javascript] it works but when i click on next for other pages it dosen’t work///

  2. Hi there,
    I am having a problem with my “content page title” in my header template I have mysitename. However I do not know how to get it to also show the content page title aswel (example if i was searching my site for a second hand boat, I would like it to come up in the title tag as “website name | Second hand boat”).

    Hope you understand what I am on about cos im well confused with it all :)

    Jason

  3. @Jason,
    In your content page’s Onload simply set the value to what ever you want the title to be.

    base.Master.Page.Header.Title = “website name | Second hand boat”;

    That should work.

  4. @pedram,
    Sorry for the very late response. But i am not sure how you would do this in PHP.

    I know some php with Zend framework. If you are using master pages, then in your master page file (.tpl), your would change your title and meta tags to somehing like this

    <title><?php echo $this->title; ?></title>

    and in your controller (since Zend is MVC and your controller sets the data for your view), you do something like

    $view->title = “The best title”;

    Good luck

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>