Meta Tags with ASP.Net?

Last post 12-02-2007 11:09 AM by BugInfested. 12 replies.

Sort Posts:

  • Meta Tags with ASP.Net?

    12-08-2006, 12:00 AM
    • Member
      15 point Member
    • TriesHard
    • Member since 10-06-2006, 4:25 AM
    • Posts 4

    This probably sounds silly, but I have run into problems with these.

    I had no problems using straight HTML editors - each page had a title, and the description was what was seen by search engines.

    But using asp.net, and a master page, it doesnt seem quite so easy. The masterpage has a description, but I cant find any way to have the same thing on each individual page. I can give each page a title, but it uses the description from the master page.

    And obviously, I dont want each page to have the same description ... there is probably an easy way to do this, but it has me beat!

    Hope someone can help. Thanks, Jonny. 

  • Re: Meta Tags with ASP.Net?

    12-08-2006, 12:47 AM
    Answer
    • Member
      242 point Member
    • dustyd
    • Member since 01-30-2006, 4:58 AM
    • Omaha, NE
    • Posts 47

    There are a couple of ways you can accomplish this...

    First, on each page in the Load, you could manually add an HtmlMeta control to the Page's header...
     

        protected void Page_Load(object sender, EventArgs e)
    {
    HtmlMeta meta = new HtmlMeta();
    meta.Name = "Description";
    meta.Content = "The description goes here...";
    Page.Header.Controls.Add(meta);
    }

     Alternatively, you could place a ContentPlaceHolder in teh HEAD of your master page. Then in each page where you want a custom Description, you could specify an asp:content, and put your meta tag in there..

    In the Master Page:

    <head runat="server">
    <title>Untitled Page</title>
    <asp:contentplaceholder id="headerPlaceHolder" runat="server">
    </asp:contentplaceholder>
    </head>

     Then, in your content pages (which reference the master page)

    <asp:Content runat="server" ContentPlaceHolderID="headerPlaceHolder">
    <meta name="Description" content="The description goes here..." />
    </asp:Content>
    Hope this helps... 
      
    Filed under: , ,
  • Re: Meta Tags with ASP.Net?

    12-24-2006, 6:22 PM
    • Member
      15 point Member
    • TriesHard
    • Member since 10-06-2006, 4:25 AM
    • Posts 4

    thank you so much Dusty!

    Im off to try this now...I had even emailed someone from Microsoft about this, but I never recieved a reply. I guess for someone experienced with coding, this may be a simple obstacle to overcome, but its a different matter for someone using to using standard HTML.

    Have a verry merry Xmas.Big Smile 

  • Re: Meta Tags with ASP.Net?

    01-23-2007, 12:18 PM
    • Member
      27 point Member
    • keiyia
    • Member since 06-23-2006, 8:29 PM
    • Posts 6

    TriesHard,

    Did you ever get this to work.  I am trying to do the exact same thing and my page is just sitting there. 

  • Re: Meta Tags with ASP.Net?

    01-23-2007, 12:26 PM
    • Member
      242 point Member
    • dustyd
    • Member since 01-30-2006, 4:58 AM
    • Omaha, NE
    • Posts 47
    keiyia - What do you mean that your page is just sitting there? Do you get an error message?
  • Re: Meta Tags with ASP.Net?

    09-18-2007, 3:22 PM
    • Contributor
      3,429 point Contributor
    • rfurdzik
    • Member since 07-01-2002, 2:32 PM
    • zikbay.com
    • Posts 1,721

    The problem with thsi solution:

    HtmlMeta meta = new HtmlMeta();
            meta.Name = "Description";
            meta.Content = "The description goes here...";
            Page.Header.Controls.Add(meta);

     

    It generates double description meta tag if there is an existing Description metatag in the master.

     How do I remove the existing one (if any) ?

  • Re: Meta Tags with ASP.Net?

    09-18-2007, 3:37 PM
    • Contributor
      2,507 point Contributor
    • omerkamal
    • Member since 02-06-2006, 2:47 PM
    • Germany
    • Posts 513

    Meta tag is not an ASP .net Control rather it is an HTML document information tag. This is why we cant access it throught ASP .NET Server side code.

  • Re: Meta Tags with ASP.Net?

    09-18-2007, 3:38 PM
    • Contributor
      3,154 point Contributor
    • pbromberg
    • Member since 06-25-2002, 9:13 AM
    • Orlando FL
    • Posts 501
    • TrustedFriends-MVPs

    do this first:

     

        foreach( Control hm1 in Page.Header.Controls )
               {
                      if (hm1 is HtmlMeta && (((HtmlMeta)hm1).Name.ToLower() == "content" || ((HtmlMeta)hm1).Name.ToLower() == "description" )  )
                    {
                        Page.Header.Controls.Remove(hm1);
                     }
               }

  • Re: Meta Tags with ASP.Net?

    09-18-2007, 4:11 PM
    • Contributor
      3,429 point Contributor
    • rfurdzik
    • Member since 07-01-2002, 2:32 PM
    • zikbay.com
    • Posts 1,721
  • Re: Meta Tags with ASP.Net?

    09-18-2007, 7:48 PM
    • Contributor
      3,429 point Contributor
    • rfurdzik
    • Member since 07-01-2002, 2:32 PM
    • zikbay.com
    • Posts 1,721

    Actually this generated error: http://test.zikbay.com//Ad/74, I do have Ajax tab on the form...:

    Server Error in '/' Application.

    Collection was modified; enumeration operation may not execute.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.InvalidOperationException: Collection was modified; enumeration operation may not execute.

    Source Error:

    Line 383:
    Line 384:                //Remove existing Description
    Line 385:                foreach (Control hm1 in Page.Header.Controls)
    Line 386:                {
    Line 387:                    if (hm1 is HtmlMeta && (((HtmlMeta)hm1).Name.ToLower() == "content" || ((HtmlMeta)hm1).Name.ToLower() == "description"))

    Source File: d:\ultimaweb\rafal\zikbay.com\wwwroot\ShowAd.aspx.cs    Line: 385

    Stack Trace:

    [InvalidOperationException: Collection was modified; enumeration operation may not execute.]
       System.Web.UI.ControlCollectionEnumerator.MoveNext() +2105138
       ShowAd_aspx.AdDetails_ItemDataBound(Object sender, RepeaterItemEventArgs e) in d:\ultimaweb\rafal\zikbay.com\wwwroot\ShowAd.aspx.cs:385
       System.Web.UI.WebControls.Repeater.OnItemDataBound(RepeaterItemEventArgs e) +105
       System.Web.UI.WebControls.Repeater.CreateItem(Int32 itemIndex, ListItemType itemType, Boolean dataBind, Object dataItem) +142
       System.Web.UI.WebControls.Repeater.CreateControlHierarchy(Boolean useDataSource) +454
       System.Web.UI.WebControls.Repeater.OnDataBinding(EventArgs e) +53
       System.Web.UI.WebControls.Repeater.DataBind() +72
       System.Web.UI.WebControls.Repeater.EnsureDataBound() +55
       System.Web.UI.WebControls.Repeater.OnPreRender(EventArgs e) +12
       System.Web.UI.Control.PreRenderRecursiveInternal() +77
       System.Web.UI.Control.PreRenderRecursiveInternal() +161
       System.Web.UI.Control.PreRenderRecursiveInternal() +161
       System.Web.UI.Control.PreRenderRecursiveInternal() +161
       System.Web.UI.Control.PreRenderRecursiveInternal() +161
       System.Web.UI.Control.PreRenderRecursiveInternal() +161
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1360
    

  • Re: Meta Tags with ASP.Net?

    12-01-2007, 2:23 PM
    • Member
      420 point Member
    • BugInfested
    • Member since 12-03-2005, 11:31 PM
    • Posts 107

    I have this in my content page:

     <meta name="Description" content="The description goes here..." />

     However, Visual Studio tells me this:

    "Cannot switch views:  Validation (XHTML 1.0 Transitional): Element 'meta' cannot be nested within element 'div'"

     I must have a DIV tag around the contentplaceholder in my masterpage:

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

    It enables me to set CSS styling.  How do I get around the Visual Studio error?

    How do I set the page Title in the Content pages and enable it for bi-lingual (Resource.resx) support?

    Do I have to use the Page_Load routine just to do these things?  If so, can you provide a VB example?  I can't believe how complicated it is to do these simple things.

     

  • Re: Meta Tags with ASP.Net?

    12-01-2007, 8:08 PM
    • Member
      184 point Member
    • VISAR
    • Member since 11-13-2005, 8:58 PM
    • The City of Taxation Without Representation
    • Posts 48

    Your page's header content placeholder is not the same as the <head> tag that appears above the <body> tag.  

    <html xmlns="http://www.w3.org/1999/xhtml">
        <head runat="server">
            <meta name="Description" content="The description of what this page is.">
    

    <title>My Page and Welcome To It!</title>

    <asp:ContentPlaceHolder id="head" runat="server">
    </asp:ContentPlaceHolder>

    </head> <body> <form id="Form1" runat="server"> <!-- your page content is here --> </form> </body> </html>
     
  • Re: Meta Tags with ASP.Net?

    12-02-2007, 11:09 AM
    • Member
      420 point Member
    • BugInfested
    • Member since 12-03-2005, 11:31 PM
    • Posts 107

    VISAR,

    I'm using a masterpage, therefore the <html>, <head> and <body> tags are in the masterpage.  I have various content pages that use the masterpage.  Each of the content pages need different page titles and meta data.  I know that I can set the page title here (highlighted) in the content pages:

    <%@ Page Language="VB" MasterPageFile="MasterPage.master" AutoEventWireup="false" CodeFile="________.aspx.vb" Inherits="_________" title="___________" %>

    But, how do I enable the title to support bi-lingual by pulling in the value from the RESX (Resource.resx, Resource.___.resx, etc.) files?

    Also, I know how to set the meta information in the code-behind (_____.aspx.vb), but isn't there an easier way to do is in the aspx page?

Page 1 of 1 (13 items)