adding meta tags

Last post 06-25-2008 8:50 AM by kunal5. 25 replies.

Sort Posts:

  • adding meta tags

    06-07-2005, 11:00 AM
    • Loading...
    • khoover
    • Joined on 10-02-2002, 1:46 PM
    • Posts 1
    Could someone tell me the proper way of adding meta tags to a page that is using a master page.

    According to the documentation (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs05/html/masterpages.asp), I should be able to do this:

    Sub Page_Load()
            Master.Page.Header.Title = "Content Title"
            Master.Page.Header.Metadata.Add("Keywords", "blah,blah")
            Master.Page.Header.Metadata.Add("Description", "blah,blah")
        End Sub

    However, "Metadata" does not seem to be a valid property for the Page.Header class.

    Has this been removed from Beta 2?  Is it planned for the final release of 2.0?
  • It BIG time changed in B2

    06-07-2005, 12:06 PM
    • Loading...
    • MorningZ
    • Joined on 07-22-2002, 2:39 PM
    • Fort Lauderdale, FL
    • Posts 1,805

    **** Edit ****
    Look at the bottom of the page for the even easier solution posted by Kristen
    ************



    Now there are Header Controls...

    First thing you need to do is add an ID to the MasterPage's <head> tag  (it should already have runat="server", if it doesn't add that too).. so just say for instance you made the ID "MasterHeader"

    Now you can programmically add items like so: 

    Dim hd As HtmlHead = CType(Page.Master.FindControl("MasterHeader"), HtmlHead)
    Dim
    mt As New HtmlMeta
    mt.Content =
    "This would be some Content Text to get added to the head tag"
    hd.Controls.Add(mt)
    mt = New HtmlMeta
    mt.Attributes.Add(
    "Keywords", "Word1,Word2,Word3")
    hd.Controls.Add(mt)


    I'm sure there's other ways, but this works for us in our early workings in Beta 2 :)

     

    "If you make it idiot proof, they'll build a better idiot"
  • A Slight Change

    06-14-2005, 6:31 PM
    • Loading...
    • Lonnie
    • Joined on 06-14-2005, 10:25 PM
    • Posts 2
    Thanks so much for the help.

    After playing with the code you wrote, I discovered that we don't have to put an id in the head tag:
    http://www.howtoadvice.com/SetMetaTags
  • Re: A Slight Change

    12-14-2005, 2:38 PM
    • Loading...
    • Lee Dumond
    • Joined on 11-03-2004, 2:51 PM
    • Decatur, IL USA
    • Posts 901

    A slightly cooler way to do it is to expose the meta data as properties of the Master Page, then set the attributes in each of your content pages with a single line of code.

    In the Master page:

    Public Property MetaDescription() As String

    Get

    Return Page.Header.Attributes.Item("Description.Content")

    End Get

    Set(ByVal value As String)

    Dim Description As New HtmlMeta

    Description.Name = "description"

    Description.Content = value

    Page.Header.Controls.Add(Description)

    End Set

    End Property

    Public Property MetaKeywords() As String

    Get

    Return Page.Header.Attributes.Item("Keywords.Content")

    End Get

    Set(ByVal value As String)

    Dim Keywords As New HtmlMeta

    Keywords.Name = "keywords"

    Keywords.Content = value

    Page.Header.Controls.Add(Keywords)

    End Set

    End Property

    Then, in the content pages:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Master.MetaDescription = "description goes here"

    Master.MetaKeywords = "keyword1,keyword2,keyword3"

    End Sub

     

    // ******************************
    if (this.PostHelpedYou)
    {
    MarkAsAnswer();
    }
  • Re: A Slight Change

    01-13-2006, 5:47 PM
    • Loading...
    • mandkcasey
    • Joined on 10-15-2003, 4:35 PM
    • Chicago, IL
    • Posts 64
    I am a little new to asp.net programming and really like the idea of only adding two lines of code for each page to discover the Meta information.  I am trying to get this to work and copied the code into my Vacation.Master file.  When I try to call it I have tried Master.MetaDescription and Vacation.Master.MetaDescription.  I keep getting a "reference to a non-shared member requires an object reference".  I think this means I am not calling it properly. Can anyone help?  I would be very appreciative.  Thanks.
  • Re: A Slight Change

    01-13-2006, 8:22 PM
    • Loading...
    • Lee Dumond
    • Joined on 11-03-2004, 2:51 PM
    • Decatur, IL USA
    • Posts 901

    Where are you exposing the properties? You have to do it in the code-behind page.

    If you have a Master page called Vacation.master, then you should also have a file called Vacation.master.vb. (Assuming you're working in Basic, of course).

    Then you expose the properties in the Vacation.master.vb class as follows:

     

    Public Property MetaDescription() As String
       Get
          Return Page.Header.Attributes.Item("Description.Content")
       
    End Get
       
    Set(ByVal value As String)
          
    Dim Description As New HtmlMeta
          
    Description.Name = "description" 
          
    Description.Content = value
          
    Page.Header.Controls.Add(Description)
       
    End Set
    End Property

    Public Property MetaKeywords() As String
       Get 
          
    Return Page.Header.Attributes.Item("Keywords.Content"
       
    End Get 
       
    Set(ByVal value As String
          
    Dim Keywords As New HtmlMeta 
          
    Keywords.Name = "keywords" 
          
    Keywords.Content = value
          
    Page.Header.Controls.Add(Keywords)
       
    End Set
    End Property

    Then you can use these properties in any content page you want. Just put these lines in the Page.Load event:

    Master.MetaDescription = "description goes here"
    Master.MetaKeywords = "keyword1,keyword2,keyword3"

    You don't need to use the word "vacation" here. The content page has a header that tells it which master page to use. If you are using Intellisense, the property names should be in the list after you type the period. If they are not, you didn't expose the properties properly.

    // ******************************
    if (this.PostHelpedYou)
    {
    MarkAsAnswer();
    }
  • Re: A Slight Change

    01-13-2006, 9:36 PM
    • Loading...
    • mandkcasey
    • Joined on 10-15-2003, 4:35 PM
    • Chicago, IL
    • Posts 64

    First, I want to thank you for the response and trying to help me through this.  Yes I am using Visual Basic and yes I have was putting the code into the vacation.master.vb file.  It looks like the following down below.  I must have it in the wrong place because it does not show after the Master. in the default.aspx.vb section under Page_Load.  Where do I need to move the code.  Once again, I appreciate the help. Mike

    Partial Class Vacation

    Inherits System.Web.UI.MasterPage

    Public Property MetaDescription() As String

    Get

    Return Page.Header.Attributes.Item("Description.Content")

    End Get

    Set(ByVal value As String)

    Dim Description As New HtmlMeta

    Description.Name = "description"

    Description.Content = value

    Page.Header.Controls.Add(Description)

    End Set

    End Property

    Public Property MetaKeywords() As String

    Get

    Return Page.Header.Attributes.Item("Keywords.Content")

    End Get

    Set(ByVal value As String)

    Dim Keywords As New HtmlMeta

    Keywords.Name = "keywords"

    Keywords.Content = value

    Page.Header.Controls.Add(Keywords)

    End Set

    End Property

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    End Class

  • Re: A Slight Change

    01-16-2006, 5:29 PM
    • Loading...
    • Lee Dumond
    • Joined on 11-03-2004, 2:51 PM
    • Decatur, IL USA
    • Posts 901

    Well, it looks like you have the code in the right place. Let me ask this -- do you actually have the content pages working in the master file? This would indicate whether or not the code that connects the content page to its master is in place. That is very important, of course.

    // ******************************
    if (this.PostHelpedYou)
    {
    MarkAsAnswer();
    }
  • Re: A Slight Change

    01-16-2006, 6:50 PM
    • Loading...
    • mandkcasey
    • Joined on 10-15-2003, 4:35 PM
    • Chicago, IL
    • Posts 64

    Yes, that I know for sure.  This is the second website I built with masterpages.  When you into the default.aspx.vb and type master. - you get a whole list of options including Application, Attributes, etc.  But I do not see the MetaDescription or the MetaKeywprds properties.  I am using a fresh Windows XP Pro build with Visual Studio 2005 Standard  No Beta has been on this machine.  Thanks for the continuous help.

  • Re: A Slight Change

    01-16-2006, 8:29 PM
    • Loading...
    • Lee Dumond
    • Joined on 11-03-2004, 2:51 PM
    • Decatur, IL USA
    • Posts 901

    Yes, that is bizarre. Maybe if I could see the pages...

    Zip the master and a sample content page together. I'm not gonna post my email publicly, so email me through the system here, and I'll emal you back. Then you'll have my real email address, and you can attach the Zip file and I'll try to take a quick look.

    // ******************************
    if (this.PostHelpedYou)
    {
    MarkAsAnswer();
    }
  • Re: A Slight Change

    01-18-2006, 6:31 PM
    • Loading...
    • KristinLaura
    • Joined on 01-18-2006, 11:26 PM
    • Sacramento, CA
    • Posts 7

    I tried adding the properties to my master page, and then changing the values via the content page (I simply cut and paste the posted code).  I get the error:  'MetaDescription' is not a member of 'System.Web.UI.MasterPage' on the content page.  Any ideas? 

    Thanks!

  • Re: A Slight Change

    01-18-2006, 8:08 PM
    • Loading...
    • Lee Dumond
    • Joined on 11-03-2004, 2:51 PM
    • Decatur, IL USA
    • Posts 901

    It means you're putting the Page_Load stuff in the Page load event of the Master page. You want to put those lines in the Page Load event of the child page.

    Remember, you are only exposing properties in the Master page. It's in the individual nested child pages that you want to use those properties.

     

    // ******************************
    if (this.PostHelpedYou)
    {
    MarkAsAnswer();
    }
  • Re: A Slight Change

    01-19-2006, 5:39 PM
    • Loading...
    • KristinLaura
    • Joined on 01-18-2006, 11:26 PM
    • Sacramento, CA
    • Posts 7

    It was in the child page.  But this is what I ended up with and it works nicely.

    In MasterPage.master:

    <head runat="server">
        <title>Untitled Page</title>
        <meta id="MetaKeywords" name="keywords" runat="server" content="" />
        <meta id="MetaDescription" name="description" runat="server" content="" />
    </head>

    In the MasterPage.master.vb

       Public WriteOnly Property Keywords() As String
            Set(ByVal value As String)
                Me.MetaKeywords.Content = value
            End Set
        End Property

        Public WriteOnly Property Description() As String
            Set(ByVal value As String)
                Me.MetaDescription.Content = value
            End Set
        End Property

    In the child page Content.aspx.vb

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Header.Title = "Content Page 1"
            DirectCast(Master, MasterPage).Keywords = ("keyword1, keyword2, keyword3")
            DirectCast(Master, MasterPage).Description = ("this is the page description")
    End Sub

  • Re: A Slight Change

    01-19-2006, 6:55 PM
    • Loading...
    • Lee Dumond
    • Joined on 11-03-2004, 2:51 PM
    • Decatur, IL USA
    • Posts 901
    KristinLaura wrote:

    It was in the child page. 

    Okay.. wait as minute. You said --

     I get the error:  'MetaDescription' is not a member of 'System.Web.UI.MasterPage' on the content page.  Any ideas? 

    It is the Master Page that inherits System.Web.UI.MasterPage. There is no way you could have gotten this error from a properly constructed content page, as that would inherit from System.Web.UI.Page.