SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist
    Join Date
    May 2006
    Posts
    457
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    adding meta tags to pages that use measter pages

    Hello all,

    how can I go about adding meta tags to a page that is using a master page?

    Code:
    <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" Title="Untitled Page" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    </asp:Content>
    Cheers

  2. #2
    SitePoint Zealot zalucius's Avatar
    Join Date
    Jul 2007
    Location
    Denmark
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Simple answer: Create a funciton for it, in the Masterpage codebehind file.

    you cannot put a content place holder in the head area, instead you can response.write() a string returned from a function.

    create the function something like this, in the masterpages codebehind:
    Code Csharp:
    public string MetaGenerator()
    {
    	string metaData = "";
    	// fetch your data from the database, put it into the metaData string...
    	// metaData = "<meta name='description' content='description'>\n";
    	// metaData += "<meta name='keywords' content='keywords'>\n";
    	return metaData;
    }

    On the masterpage .aspx page, you can then output the metaData like this:
    Code ASP:
    <head runat="server">
        <title></title>
    	<&#37; Response.Write(MetaGenerator()); %>
    </head>

    The code will output the result of MetaGenerator() inside the head section, all you have to do, is make sure the string metaData is formatted correct, and ofcourse make sure it will retrieve the correct data from the database.
    Last edited by zalucius; Aug 13, 2007 at 10:12. Reason: added information
    zalucius

  3. #3
    SitePoint Wizard
    Join Date
    Feb 2007
    Posts
    1,274
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The following code can be used from any page, content, master or standalone:

    Code Csharp:
        HtmlMeta m = new HtmlMeta();
        m.HttpEquiv = "Refresh";
        m.Content = "5";
        Header.Controls.Add(m);

    The <head> tag is exposed as the Header property of a page. The HtmlMeta class nicely embodies the meta tag which goes into the header as a child control.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •