Add the description meta tag on a web page created with master page

Last post 11-25-2009 5:33 PM by tugberk_ugurlu_. 6 replies.

Sort Posts:

  • Add the description meta tag on a web page created with master page

    11-25-2009, 9:18 AM

    Hi ! I need give different description meta tags for my each aspx page but since I use the master page, I cannot add it or I do not know to add it? Is there any way to do that? Or Do I need to define a content place holder into meta tag section?

    Tugberk Ugurlu (WWD 2008 Express Edition / VB)
  • Re: Add the description meta tag on a web page created with master page

    11-25-2009, 9:30 AM
    Answer
    • All-Star
      32,472 point All-Star
    • augustwind
    • Member since 07-21-2002, 11:16 PM
    • Garland, TX
    • Posts 4,546
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs

    create a class which inherits PAGE, that each of your content pages inherits from. Create properties in the BasePage for each meta tag you want to use:

    Public Property MetaDescription() As String
            ' Page description meta tag used by search engines
            Get
                Return Nothing
            End Get
            Set(ByVal value As String)
                Dim meta As New HtmlMeta
                meta.Name = "Description"
                meta.Content = value
                Page.Header.Controls.Add(meta)
            End Set
        End Property
     
    then, in the Init Event of each content page, put something like:
    myBase.Metadescription="Here's my Description"
    David Wier
    MCP/ASPInsider
    ASPNet101.com - where to look first!
    Replace It! - the newest from August Wind - search/replace in multiple files
    Control Grouper - easily control properties for multiple controls with one control!
    Calendar Express - The Best HTML Calendar Generator on the web!
    (Please 'Mark as Answer' when it applies)
  • Re: Add the description meta tag on a web page created with master page

    11-25-2009, 9:38 AM

    Just what I need ! do I need to add the VB code into master page VB file or into the aspx page which I need define the meta tag? 

    Tugberk Ugurlu (WWD 2008 Express Edition / VB)
  • Re: Add the description meta tag on a web page created with master page

    11-25-2009, 12:40 PM

    Hi there ! I tried it but I guess I did something wrong? Where exactly do I need to add this codes? 

    Tugberk Ugurlu (WWD 2008 Express Edition / VB)
  • Re: Add the description meta tag on a web page created with master page

    11-25-2009, 3:18 PM

    Hi there ! Where do I need to add below code;

    Public Property MetaDescription() As String  
            ' Page description meta tag used by search engines   
            Get  
                Return Nothing  
            End Get  
            Set(ByVal value As String)   
                Dim meta As New HtmlMeta   
                meta.Name = "Description"  
                meta.Content = value   
                Page.Header.Controls.Add(meta)   
    End Set  
     End Property 


    How can I set Base... Properties. I am kind of new with this kind of thing can you help me over with details. Thanks ! 

    Tugberk Ugurlu (WWD 2008 Express Edition / VB)
  • Re: Add the description meta tag on a web page created with master page

    11-25-2009, 4:01 PM

    I have found this one on the web but do not know where I put them;

    Adding a literal contorl, creating HtmlMeta first and then adding it to controls
    both works.

    e.g.

    Page.Header.Controls.Add(newLiteralControl(@"<meta name='keywords' content='keyword1, keyword2' />"));


    OR

    HtmlMeta meta = newHtmlMeta();
    meta.Name =
    "keywords";
    meta.Content =
    "keyword1, keyword2";
    Page.Header.Controls.Add(meta);

    I tried to put it insed the init event but it did't work. Also I came across this below one;

    Far from it, actually. Master pages are a godsend. Although, they do introduce their fair share of issues...

    What you can do is make the <head> tag a server control in the master page:

    <head id="MyHead" runat="server">

    then you can add controls to it.

    (untested off the top of my head code...)
    MyHead.Controls.Add(new LitrelControl("<meta name=\"description\" content=\"Your Description\" />"));

     

    The above is useless as well. Please urgent help ! 

    Tugberk Ugurlu (WWD 2008 Express Edition / VB)
  • Re: Add the description meta tag on a web page created with master page

    11-25-2009, 5:33 PM
    Answer

    The Below one works;

    Step 4: Adding Other Page-Specific Markup to the <head> Section

    Steps 1, 2, and 3 looked at customizing the <title> element on a page-by-page basis. In addition to <title>, the <head> section may contain <meta> elements and <link> elements. As noted earlier in this tutorial, Site.master's <head> section includes a <link> element to Styles.css. Because this <link> element is defined within the master page, it is included in the <head> section for all content pages. But how do we go about adding <meta> and <link> elements on a page-by-page basis?

    The easiest way to add page-specific content to the <head> section is by creating a ContentPlaceHolder control in the master page. We already have such a ContentPlaceHolder (named head). Therefore, to add custom <head> markup, create a corresponding Content control in the page and place the markup there.

    To illustrate adding custom <head> markup to a page, let's include a <meta> description element to our current set of content pages. The <meta> description element provides a brief description about the web page; most search engines incorporate this information in some form when displaying search results.

    A <meta> description element has the following form:

    <meta name="description" content="description of the web page" />

    To add this markup to a content page, add the above text to the Content control that maps to the master page's head ContentPlaceHolder. For example, to define a <meta> description element for Default.aspx, add the following markup:

    <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">     <meta name="description" content="Welcome to Scott Mitchell's Master Page Tutorials series." /> </asp:Content>

    Because the head ContentPlaceHolder is not within the HTML page's body, the markup added to the Content control is not displayed in the Design view. To see the <meta> description element visit Default.aspx through a browser. After the page has been loaded, view the source and note that the <head> section includes the markup specified in the Content control.

    http://www.asp.net/Learn/master-pages/tutorial-03-vb.aspx

    Tugberk Ugurlu (WWD 2008 Express Edition / VB)
Page 1 of 1 (7 items)