How to set page description dynamically from either the Master page or a Module?

Last post 12-06-2008 12:01 PM by goixq. 4 replies.

Sort Posts:

  • How to set page description dynamically from either the Master page or a Module?

    12-05-2008, 3:20 PM
    • Member
      31 point Member
    • goixq
    • Member since 10-09-2006, 11:12 AM
    • Posts 107

    Hello,

    How can I set page description dynamically from either the Master page or a Module?

    It sounds simple but I have yet to find an answer.

  • Re: How to set page description dynamically from either the Master page or a Module?

    12-05-2008, 4:03 PM
    • All-Star
      21,147 point All-Star
    • bullpit
    • Member since 06-29-2006, 3:59 PM
    • Posts 4,670

    Which page description are you referring to?

  • Re: How to set page description dynamically from either the Master page or a Module?

    12-05-2008, 9:37 PM
    Answer
    • Contributor
      4,650 point Contributor
    • novicehere
    • Member since 01-28-2008, 7:21 PM
    • New Hampshire, US
    • Posts 852

    Hello There,

    if you are trying to set a content page description, you can do something like this.

    First we create a master page called MetaTagsDynamically.master

    <%@ Master Language="C#" AutoEventWireup="true" CodeFile="MetaTagsDynamically.master.cs" Inherits="common_MetaTagsDynamically" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >

    <head runat="server" id="mainHead"> ----- here we are giving a id to the head tag

    <title>Meta Tags Dynamically</title>

    <meta name="DESCRIPTION" content="This is the master page description " /> this is description for master page

    </head>

    <body>

    <form id="form1" runat="server">

    <div>

    <asp:contentplaceholder id="cpMain" runat="server"></asp:contentplaceholder>

    </div>

    </form>

    </body>

    </html>

    Now the content page called DynamicPageDesc.aspx

    <%@ Page Language="C#" MasterPageFile="~/common/MetaTagsDynamically.master" AutoEventWireup="true" CodeFile="DynamicPageDesc.aspx.cs" Inherits="General_DynamicPageDesc" Title="Dynamic Page Description" %>

    <asp:Content ID="Content1" ContentPlaceHolderID="cpMain" Runat="Server"></asp:Content>

    In CODE BEHIND OF DynamicPageDesc.aspx , i am adding the page descripiton on the page_load event

     protected void Page_Load(object sender, EventArgs e)
        {
            //First we find  the Head Tag in Master Page        
            HtmlHead headMain = (HtmlHead)Page.Master.FindControl("mainHead");
            HtmlMeta htMeta = new HtmlMeta();
            htMeta.Attributes.Add("name", "description");
            htMeta.Attributes.Add("content", "here you can type page description");
           
            //adding  Meta Tag to Head                
            headMain.Controls.Add(htMeta);
            
            //Similiarly we can add keyword Meta Tag to Head Section        
            HtmlMeta htMeta1 = new HtmlMeta();
            htMeta1.Attributes.Add("name", "keywords");
            htMeta1.Attributes.Add("content", "Here you can type page keywords");
            
            // adding the Meta Tag to Head
            headMain.Controls.Add(htMeta1);
        }

     now if you run the page and view source, this is what you will get

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="ctl00_mainHead"><title>
    	Dynamic Page Description
    </title><meta name="DESCRIPTION" content="This is the master page description " /> ---------- coming from the master page
    <meta name="description" content="here you can type page description" />
    <meta name="keywords" content="Here you can type page keywords" />------------------------coming from the content page
    </head>
    <body>
        <form name="aspnetForm" method="post" action="DynamicPageDesc.aspx" id="aspnetForm">
    <div>
    <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTEwMDUyNjYzMjhkZNFkZUOSU4P6hz2M5dCGdXutqn7l" />
    </div>
    
        <div>
            
    
        </div>
        </form>
    </body>
    </html>
    
    Hope this helps
    Thanks
     
     

     

    Keyboard not found. Please Press < F1 > to RESUME

    Please Remember to Mark as Answer for the post(s) that help you.....so it can help others......Thanks
  • Re: How to set page description dynamically from either the Master page or a Module?

    12-05-2008, 10:53 PM
    • Member
      146 point Member
    • xuannien84
    • Member since 07-01-2008, 4:06 PM
    • Posts 66

    Hi,

    You can use

    HtmlMeta meta = new HtmlMeta();
            meta.Name = "descriptions";
            meta.Content = content;
            Header.Controls.Add(meta);

  • Re: How to set page description dynamically from either the Master page or a Module?

    12-06-2008, 12:01 PM
    • Member
      31 point Member
    • goixq
    • Member since 10-09-2006, 11:12 AM
    • Posts 107

    Hi novicehere,

    Thank you for your elaborate explanation on how to implement page description. Your solution works perfectly.

Page 1 of 1 (5 items)