Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
Tutorial RSS
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - Performance - Dynamically changing META Tags with MasterPages (C#)
Dynamically changing META Tags with MasterPages (C#)


ASP.NET Performance Tutorial

How to dynamically change meta tags when using MasterPage in ASP.NET and C#

If you have lots of dynamic pages instead of static, you could be losing out on how your pages are indexed. For example, if you use lots of querystrings, the chances are that all pages (or instances of that page) have the same meta tags (title, description, keywords). To overcome this, we can set up our site so that we can set these meta tags dynamically (upon certain actions). If using a MasterPage, this can get a little trickier, but can still be done. This tutorial shows how.
Firstly, on our MasterPage, we give our meta tags and the page title an ID, and runat attribute:

<title id="PageTitle" runat="server">This is the Default Page Title...</title>
<meta name="Keywords" id="PageKeywords" content="default, page, keywords" runat="server" />
<meta name="Description" id="PageDescription" content="This is the Default page desctription, which should be changed when the page is loaded." runat="server" />

We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!

Next, we put the following code in MasterPage code-behind:

public string MetaTitle
{
get
{
return PageTitle.Text;
}
set
{
PageTitle.Text = value;
}
}
public string MetaKeywords
{
get
{
return PageKeywords.Content;
}
set
{
PageKeywords.Content = value;
}
}
public string MetaDescription
{
get
{
return PageDescription.Content;
}
set
{
PageDescription.Content = value;
}
}

Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!

The Default.aspx (content page) looks like the following:

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
Upon loading of this page, the new Page Title, Description and Keywords meta tags should be displayed. You can check this by viewing the browser window title.<br />
<br />
This small area of the page is a content page, the background is the master page.
</asp:Content>

We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!

Then in the code-behind of Default.aspx we have the following, which will set the meta tags:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
MasterPage myMaster = (MasterPage)this.Master;
myMaster.MetaTitle = "This is the new Page Title, which is set upon Page_Load";
myMaster.MetaDescription = "This is the new Page Description, which is set upon Page_Load";
myMaster.MetaKeywords = "new, page, keywords, set, upon, page, load";
}
}

Download the Full Working Version of this Project written with Visual Studio.NET C# 2005 Here!

Looking for more ASP.NET Tutorials? Click Here!





 
  Developer Resources







Server Intellect Rocks