LearnFast.Ninja

Add meta description to master page and access it from all pages

If you not using ASP.NET 4.0 you can't use MetaDescription and MetaKeyword properties inside Page Class, like you do with this.Title. So Here the good and old fashion style to how you add meta description and meta keywords to your Master Page via the child page. Follow this steps and then you will be the ability of changing the meta description from all child pages.

Step 1: Create public property in the C# file MainMaster.master.cs.

public partial class MainMaster : System.Web.UI.MasterPage

{

    public string Meta_Description { getset; }


Step 2: Add the parameter to the head tag with in line coding in MainMaster.master.

<head runat="server">

    <title></title>

    <%=Meta_Description %>


Step 3: Access the property from the child page.

protected void Page_Load(object sender, EventArgs e)

{

    ((MainMaster)this.Master).Meta_Description = "<meta name=\"description\" content=\"Page description.\" />";


Result:


For experts: Build an interface then make the child pages to implement it.

public interface IPage_With_Meta_Tags

{

    void Set_Title(string Title);

    void Set_Meta_Description(string Description);

    void Set_Meta_Keywords(string Keywords);

}

// --------------------------------------------------


public partial class MainMaster : System.Web.UI.MasterPage

{

    public string Meta_Description { getset; }

    public string Meta_Keywords { getset; }

// --------------------------------------------------


<%@ Master Language="C#"

<head runat="server">

    <title></title>

    <%=Meta_Description %>

    <%=Meta_Keywords %>

// --------------------------------------------------


public partial class _Default : System.Web.UI.PageIPage_With_Meta_Tags

{

    protected void Page_Load(object sender, EventArgs e)

    {

        // IPage_With_Meta_Tags --------------------------------------

        Set_Title("Page Title ");

        Set_Meta_Description("Page description");

        Set_Meta_Keywords("asp.net, html, javascript");

    }

 

    #region IPage_With_Meta_Tags

    public void Set_Title(string Title)

    {

        this.Title = Title;

    }

 

    public void Set_Meta_Description(string Description)

    {

        ((MainMaster)this.Master).Meta_Description = "<meta name=\"description\" content=\"" + Description + "\" />";

    }

 

    public void Set_Meta_Keywords(string Keywords)

    {

        ((MainMaster)this.Master).Meta_Keywords = "<meta name=\"keywords\" content=\"" + Keywords + "\" />";

    }

    #endregion


Tags

asp.net

meta-description description master-page keywords meta interface