Meta tag closing "/" gets removed when ASP.Net page is rendered.

Last post 11-28-2008 4:45 PM by novicehere. 4 replies.

Sort Posts:

  • Meta tag closing "/" gets removed when ASP.Net page is rendered.

    08-30-2007, 2:42 PM
    • Member
      2 point Member
    • jtkennedy
    • Member since 08-30-2007, 6:25 PM
    • Posts 3

    I've come across a weird problem when my ASP.Net page is rendered. The closing "/" gets removed when the page is rendered. I am using master and content pages. I have tracked down the problem to the head section is specified to runat="server". If I remove this, the meta tag is rendered correct, but this is required so that the content pages set the title to each page. I have also tried dynamically adding the meta tags from with the page_load event of the content page, but I get the same results. The reason I want this corrected is because the w3c validator is saying the meta tag isn't closed. Any ideas?

    Master Page Code: 

    <%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head runat="server">
        <title>The Kennedyz</title>
        <meta name="description" content="Official website of the Kennedy family." />
        <link href="./MasterPageSS.aspx" type="text/css" rel="stylesheet" />
        <link href="./ContentPlaceHolderSS.aspx" type="text/css" rel="stylesheet" />
    </head>
     

    Gets rendered to (notice the closing "/" has been removed from the meta tag):

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en">
    <head>
    <title>
    Welcome to the Kennedyz family website!
    </title>
    <meta name="description" content="Official website of the Kennedy family.">
    <link href="./MasterPageSS.aspx" type="text/css" rel="stylesheet" />
    	<link href="./ContentPlaceHolderSS.aspx" type="text/css" rel="stylesheet" />
    </head>
  • Re: Meta tag closing "/" gets removed when ASP.Net page is rendered.

    08-30-2007, 3:24 PM
    Answer
    • Member
      40 point Member
    • ManojSharma
    • Member since 08-30-2007, 6:54 PM
    • Posts 5

    Hi

    You could add the meta tag programmatically.

    For example, in the Page_Load event, you could use the following line:

    // add meta content to the head section of the page.
    this.Header.Controls.Add(
      new LiteralControl("<meta name='description' content='Official website of the Kennedy family.' />"));

    Happy coding!

    Regards,

    MANOJ KUMAR SHARMA.

    Filed under: ,
  • Re: Meta tag closing "/" gets removed when ASP.Net page is rendered.

    08-30-2007, 8:18 PM
    • Member
      2 point Member
    • jtkennedy
    • Member since 08-30-2007, 6:25 PM
    • Posts 3

    Thanks a bunch! It worked like a charm!

    Have a great day!

    J.T.

  • Re: Meta tag closing "/" gets removed when ASP.Net page is rendered.

    11-28-2008, 4:05 PM
    • Member
      292 point Member
    • johnly
    • Member since 05-31-2008, 12:23 PM
    • Bangalore, India
    • Posts 56

    If you are planning to change the meta tags programatically, then I have a simple solution for you. 

    As you know that in ASP.Net you always have the ability to add the attribute: Runat="server" .

    Using this attribute, you can access your controls at runtime, even for the tags that are in the <head></head> portion of your page (which include meta and title tags)

    Go to your HTML code page, and modify the title tag to look like:

    <title id="pgTitle" runat="server">Some Title Here</title>

    Then add the following two meta tags to your code under the title tag:

    <meta name="description" content="description" id="metaDescription" runat="server" />
    <meta name="keywords" content="keys" id="metaKeywords" runat="server" />

    So, we have just set for every tag an ID, and added the runat="server" attribute. This way we can access these controls from the code behind page.

    Double click on the page to go to the "OnLoad" event. Place there the following:

    pgTitle.InnerText = "New Page Title created on runtime "
    metaKeywords.Attributes("content") = "these, are, my keywords, set on, runtime"
    metaDescription.Attributes("content") = "This is the text for description meta tag set on runtime"

    Now save the page and load it in browser to see the effect.

     

    Johnlee Sam
    Zebralive | Send BulkSMS
  • Re: Meta tag closing "/" gets removed when ASP.Net page is rendered.

    11-28-2008, 4:45 PM
    • Contributor
      4,650 point Contributor
    • novicehere
    • Member since 01-28-2008, 2:21 PM
    • New Hampshire, US
    • Posts 852

    Hello There,

    As far as i know Meta Tag is a one sided tag in HTML, it does not require a closing tag.

    Thanks

     

    Keyboard not found. Please Press < F1 > to RESUME

    Please Remember to Mark as Answer for the post(s) that help you.....so it can help others......Thanks
Page 1 of 1 (5 items)