Alex
Alex

Reputation: 3968

Setting title tags using asp.net

I am trying to set a set meta data programatically.

I have the following setup on my asp.net master page:

<asp:ContentPlaceHolder ID="MetaTags" runat="server">
    <meta name="description" content="">
</asp:ContentPlaceHolder>

and I then have this on an individual page:

<asp:Content runat="server" ContentPlaceHolderID="MetaTags">    
<title runat="server" id="pageTitle"></title>
<meta runat="server" id="pageDescription" name="description"/>  
<meta runat="server" id="pageKeywords" name="keywords" />    
</asp:Content>

And this in the C# code:

    pageTitle.InnerText = "TITLE";
    pageDescription.InnerText = "DESC";
    pageKeywords.InnerText = "KEY";

Which generates this on the page:

<title id="ctl00_MetaTags_pageTitle">TITLE</title>
<meta id="ctl00_MetaTags_pageDescription" name="description">DESC</meta>  
<meta id="ctl00_MetaTags_pageKeywords" name="keywords">KEY</meta> 

I am doing it like this because my understanding is because I am using:

<asp:ContentPlaceHolder ID="MetaTags" runat="server">

I cannot have a <head> tag on individual pages - because it will cause duplicate header tags?

I tried using:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="###"  MasterPageFile="#####" Title = ""%>

and in C# Page.Title = "Title"

but this did not display anything.

And separately Master.Page.Title = "Title";

but this did not work either.

So, 2 parts to my question;

1.Does anyone know another way of adding meta data to the page programmatically.

2.If I do it this way, and the meta data has id's eg:

<title id="ctl00_MetaTags_pageTitle">TITLE</title>

Does this invalidate the title tag.

Upvotes: 0

Views: 793

Answers (1)

Rion Williams
Rion Williams

Reputation: 76557

1.Does anyone know another way of adding meta data to the page programmatically.

You could accomplish this by simply creating a Placeholder and then building your individual meta tags in your code-behind and adding them to it :

<head runat="server">
    <title>Your Example Page</title>
    <!-- Add your Meta tags into this container -->
    <asp:PlaceHolder ID="Meta" runat="server" />
</head>

And then within your code-behind, you could build these as HtmlMeta controls and simply insert them as expected :

// Build each of your meta tags and add them
var tags = new List<HtmlMeta>() {
     new HtmlMeta() { Name = "description",Content = "Your Description Here" },
     new HtmlMeta() { Name = "keywords", Content = "a,b,c,d" },
     new HtmlMeta() { Name = "robots", Content = "index,follow" }
};

// Add each of them to your placeholder
foreach(var tag in tags)
{
     Meta.Controls.Add(tag);
}

which would render as follows in your <head> section :

<head>
    <title>Your Example Page</title>
    <meta name="description" content="Your Description Here">
    <meta name="keywords" content="a,b,c,d">
    <meta name="robots" content="index,follow">
</head>

You could easily adjust this as you see fit.

2.If I do it this way, and the meta data has id's ... Does this invalidate the title tag?

IIRC, I don't believe that this will have any adverse effect on the actual <meta> tag working as expected, although the spec doesn't specify it as a valid tag. Generally though, you should probably avoid adding them if possible.

Upvotes: 1

Related Questions