How to stop ASP.NET inserting another <title> tag in my master psge?

A

Alan Silver

Hello,

I am just experimenting with master pages, and am trying to add a
content placeholder in the <head> section, so that individual pages can
set their own page title and meta tags. The master page looks like
this...

<%@ Master Language="C#" AutoEventWireup="true"
CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<asp:ContentPlaceHolder ID="cphHead" runat="server" />
</head>
<body>
.... other stuff with content placeholders here...
</body>
</html>

A page that uses this master would have content controls for the
placeholder in the header, as well as the others in the body (not
shown). For example (partial code for a sample .aspx page shown)...

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="About.aspx.cs"
Inherits="About" Title="Untitled Page" debug="true"
MasterPageFile="MasterPageToys.master" %>
<asp:Content ID="cntHead" ContentPlaceHolderID="cphHead" Runat="Server">
<title>Test Page Using Master Pages</title>
<meta name="keywords" content="stuff, nonsense, master pages, asp.net">
<meta name="description" content="This is a page using master pages,
etc">
</asp:Content>
....other content controls here...

The problem is that when the page is rendered, ASP.NET adds another
<title> tag, giving invalid HTML like this...

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Test Page Using Master Pages</title>
<meta name="keywords" content="stuff, nonsense, master pages, asp.net">
<meta name="description" content="This is a page using master pages,
etc">
<title>
Untitled Page
</title></head>
<body>

Is there any way to stop it doing this, or am I going about this the
wrong way? Any comments appreciated.
 
A

Alan Silver

The problem is that when the page is rendered, ASP.NET adds another
<title> tag, giving invalid HTML like this...
<snip>

Since posting, I discovered that the page itself exposes a Title
attribute in the page directive, and the Page object has a corresponding
Title property, so the title can either be set in the .aspx file or in
the code-behind.

However, it seems that setting the meta tags is a lot more work. The
example I showed used a content control with the meta tags in, which is
quick and simple. Doing this programmatically looks like this...

HtmlMeta hm1 = new HtmlMeta();
// Get a reference to the page header element.
HtmlHead head = (HtmlHead)Page.Header;
// Define an HTML <meta> element that is useful for search engines.
hm1.Name = "keywords";
hm1.Content = "words that describe your web page";
head.Controls.Add(hm1);

which is a lot of work for such a simple task!!

So, is there any disadvantage to doing it the way I showed? Even having
a literal control in the .aspx file's content control would be less
lines than the way shown above.

Any comments?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top