By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,925 Members | 732 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,925 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.

--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
>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?

--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.