Page view counter

Different head and meta tag content on each content page

Rate It (1)

Last post 09-11-2007 8:47 PM by tymberwyld. 10 replies.

Sort Posts:

  • Different head and meta tag content on each content page

    08-31-2006, 5:31 AM
    • Loading...
    • ajw
    • Joined on 08-29-2006, 4:16 PM
    • UK
    • Posts 392
    • Points 611

    Hi,

     I am using master pages.  I want to put different head and meta (description, keywords) on each content page. How can this be done to make my site better designed for search engines.

     

    Thanks

    Andrew Welch

     

    Abtec Network Systems

    Current project: Cycle round the world
  • Re: Different head and meta tag content on each content page

    08-31-2006, 10:46 AM
    • Loading...
    • hoopslife
    • Joined on 12-29-2005, 2:29 PM
    • Fort Myers, FL
    • Posts 267
    • Points 1,305
  • Re: Different head and meta tag content on each content page

    08-31-2006, 11:03 AM
    • Loading...
    • ajw
    • Joined on 08-29-2006, 4:16 PM
    • UK
    • Posts 392
    • Points 611

    I am really more interested in having a different title and meta keywords and description on each page or for each page rather than styles.

     

    Cheers

    Andrew

    Current project: Cycle round the world
  • Re: Different head and meta tag content on each content page

    08-31-2006, 6:56 PM

    I'm still new to master pages, but I know of two ways to control a master pages title or meta tags.  One was mentioned in a previous post (adding a content placeholder to the head section of the master page).  Here is another that someone showed to me:

    The following code in the code behind file of the master page:

    HtmlMeta theMetaTag = new HtmlMeta();
    theMetaTag.Attributes.Add("name", "test");
    theMetaTag.Attributes.Add("content", "this is a test");
    Page.Header.Controls.Add(theMetaTag);

    will generate this tag in the head section of the page:

    <meta name="test" content="this is a test" />

    In order to give more control (if that is necessary), create a public property in the master page that controls this meta tag.  For instance, I have worked on pages where we have boolean properties to tell whether or not an optional meta tag is rendered, or string properties to tell what should go in the content attribute of a required meta tag.

    So if you want the meta tag to be optional, add this property to the master page:

    public property bool UseMetaTag
    {
       get
       {
          object o = ViewState["usemetatag"];
          return o==null?string.empty:(string)o;
       }
       set
       {
          ViewState["usemetatag"] = value;
       }
    }

    (I am doing this from memory so I hope I got that right.)


    Then you just set it in the content page's code behind:

    Master.UseMetaTag = true;

    and make the code above conditional:

    if (this.UseMetaTag)
    {
       HtmlMeta theMetaTag = new HtmlMeta();
       ...
    }

    As far as setting the title dynamically, I created this property in a master page and set it in the content page:

        public String PageTitle
        {
            get
            {
                return Page.Header.Title;
            }
            set
            {
                Page.Header.Title = value;
            }
        }

     

    I hope this is at least somewhat helpful:) 

    I would, of course, be interested if someone knows of a better or different way.

  • Re: Different head and meta tag content on each content page

    08-31-2006, 10:29 PM
    • Loading...
    • lostlander
    • Joined on 05-22-2006, 11:55 AM
    • Posts 607
    • Points 3,041

    Here is an article you can follow:

    In the section: Headers, Scripts, and Meta Tags, Too

    ASP.Net 2.0 - Master Pages: Tips, Tricks, and Traps

    It details the process of adding Headers and other things.

    Hope it helps! ^_^

  • Re: Different head and meta tag content on each content page

    09-01-2006, 7:13 AM
    • Loading...
    • ajw
    • Joined on 08-29-2006, 4:16 PM
    • UK
    • Posts 392
    • Points 611

    Hi, thanks for all the info.  I am still having problems integrating this into my site.  My set up is I have one master page and other content pages such as aboutus.aspx, contact.aspx, etc.  Would it be possible for you to spell it out for me where the code goes exactly.  Thanks for your patience>

     

    Andrew

    Current project: Cycle round the world
  • Re: Different head and meta tag content on each content page

    09-03-2006, 8:37 AM
    • Loading...
    • ewschone
    • Joined on 08-23-2006, 9:10 PM
    • The Netherlands
    • Posts 11
    • Points 49
    ajw:

    Hi, thanks for all the info.  I am still having problems integrating this into my site.  My set up is I have one master page and other content pages such as aboutus.aspx, contact.aspx, etc.  Would it be possible for you to spell it out for me where the code goes exactly.  Thanks for your patience>

     I have been playing a bit with this and you can try something like this in the page load of contact.aspx page:

               Dim myMeta = New HtmlMeta
               myMeta.name = "description"
               myMeta.content = "This is our contact page"
               Page.Header.Controls.Add(myMeta)

    Odd enough i didnt get any intellisense for myMeta Huh?.

     

     

    http://ericdotnet.wordpress.com
  • Re: Different head and meta tag content on each content page

    09-04-2006, 4:19 AM
    Answer
    • Loading...
    • ajw
    • Joined on 08-29-2006, 4:16 PM
    • UK
    • Posts 392
    • Points 611
    I dont know whether its a good way of doing it but I added a content placeholder in the head tag of the master page which I then linked to in each of my content pages.  It might not work very well for search engines though because the content doesnt sit in the head tags on the content pages because the head tags are located on the master page.  Even though when the page is compiled and you go view source on the page when viewing it in the web browser it does show up in the head tags.  However, if google doesnt compile the page before crawling it then this wont happen if you see what i mean.
    Current project: Cycle round the world
  • Re: Different head and meta tag content on each content page

    09-12-2006, 7:16 AM
    • Loading...
    • LudovicoVan
    • Joined on 12-02-2004, 3:01 PM
    • The World's End
    • Posts 1,725
    • Points 8,569

    ajw:

    > I dont know whether its a good way of doing it but I added a content placeholder in the head tag of the master page which I then linked to in each of my content pages.

    IMO, this is a great solution!

    > It might not work very well for search engines though because the content doesnt sit in the head tags on the content pages because the head tags are located on the master page.

    This problem actually does not exist and your pages are fine. Consider that master and content get merged before being sent back to the browser or crawler, so it just looks like a regular page and the client has not even way to ascertain it is coming from a page implementing a master...

    Hope this clearifies. -LV

    Julio P. Di Egidio
    Software Analyst Programmer
    =BUSINESS AND SCIENTIFIC=
    =SOFTWARE DEVELOPMENT=
    http://julio.diegidio.name

    (Peace X Love] = [++1)
    Filed under: ,
  • Re: Different head and meta tag content on each content page

    01-17-2007, 1:29 PM
    • Loading...
    • chazcross
    • Joined on 12-29-2005, 4:03 PM
    • Troy
    • Posts 4
    • Points 14

    i went a different route.

     

    In the head of the masterpage i added a id and runat tag

    <meta id="description" runat="server" name="description"   content="Default description here" />


    And for every page where i needed to modify this tag i used
     

    HtmlMeta description = (HtmlMeta)Page.Header.FindControl("description");
     description.Content = "description here";
     
    Nerd, Geek, Online Guru Programming Master and ASP.NET Rockstar. Need I say more?
  • Re: Different head and meta tag content on each content page

    09-11-2007, 8:47 PM
    • Loading...
    • tymberwyld
    • Joined on 08-11-2006, 9:41 AM
    • Posts 11
    • Points 23

     Umm, "Title" and "Description" are already Attributes on each Content page's "Page" tag

    <%@ Page Language="C#" MasterPageFile="~/Master/Site.Master" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="MySite.Login" Title="Untitled Page" Description="" %> 

     

    You'd just need to add Meta, Styles, and Links (JavaScript) as other properties if need be...

Page 1 of 1 (11 items)