Changing Meta tags dynamically
Firstly, on our MasterPage, we give our meta tags and the page title an ID, and runat attribute:
Next get/set properties in 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;
}
}
The .cs of a Default.aspx page whose meta tag has to be changed is :
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";
}
}
