Duplicate title tags using ASP.NET MasterPage
Asked Answered
T

8

5

I need to set the title of a page dynamically, and so I use code similar to the following:

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="~/about.aspx.cs" Inherits="Default" %>
<%@ Register Assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="ajaxToolkit" %>
<%@ MasterType VirtualPath="MasterPage.master" %>
<%@ OutputCache Duration="43200" VaryByParam="*" Location="Server" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
 <title><%=pageTitle%></title>
</asp:Content>

But this generates duplicate title tags. Is there any way I can get around this? Thanks.

EDIT: Following from the suggestions below, I now have the following in my MasterPage:

<head id="Head1" runat="server">
<title>Default Title</title>
...
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head> 

and the following in my primary page:

    this.Title="xxx";

but I'm not getting any title (neither "Default Title" nor "xxx").

EDIT: Nevermind. Got it working using that method.

Territus answered 20/1, 2010 at 15:7 Comment(0)
P
8

The header of your .master needs to look like this:

<head runat="server">
<title>Default Title</title>
  .. other tags
</head>

Then in your page code-behind in the Page_Load, you write:

protected void Page_Load(object sender, EventArgs e)
{
    this.Title = "My Page Title";
}
Plumcot answered 20/1, 2010 at 15:20 Comment(0)
S
11

If the second title tag was empty and at the end of the head tag, then what is happening is that the Head control (note the runat="server") cannot see a Title control within it's controls, and therefore adds an empty Title tag to the end.

Most of the answers here are valid, but for minimal change what you could do is add the following to your head control-

<title id="Title1" visible="false" runat="server"><%-- hack to turn the auto title off --%></title>

Other options include-

  • Take the runat="server" off the head control, but this will mean you cannot use server side controls
  • Use a Title control and set it programatically, like your solution.

I like the hidden title tag, because it allows you to use the ContentPlaceholder approach, which keeps it in the template. It also allows you to use a custom control to get the title (in our case, we're using a 3rd party library to pull the title from a DB)

See this blog post by Phil Haack for the background on this.

Streeto answered 8/8, 2011 at 6:9 Comment(2)
cool and perfect trick.. i needed exactly this cos am doing screen scrapping and I don't want the default title tag appear in my pages.. Thanks..Interact
Cool, that first link is to my blog. Just thought I'd try to claim some credit here :)Keeler
P
8

The header of your .master needs to look like this:

<head runat="server">
<title>Default Title</title>
  .. other tags
</head>

Then in your page code-behind in the Page_Load, you write:

protected void Page_Load(object sender, EventArgs e)
{
    this.Title = "My Page Title";
}
Plumcot answered 20/1, 2010 at 15:20 Comment(0)
I
5

To avoid the duplicate tags just do the following, no extra code.

In your MasterPage have something like this:

<head>
    <title></title>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>

And on every other page add the Title="Your Title" attribute to the page directive:

<%@ Page Title="Your Title" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

This eliminates your duplicate tags and puts the title in a clear, visible place at the top in code view.

Hope this helps.

Industrials answered 18/3, 2010 at 19:23 Comment(0)
P
0

Try this instead

        ((MyPageClass)Page).Title = "Your Page Title"

This code goes in the Page_Load of your Master page. I have to use a cast ("MyPageClass") because I derive my own PageClass so I can put strongly typed session objects there. If you don't, then you won't need the cast.

If you need to do this at the page level, I think you can just use:

     Title = "Your Page Title"

in your Page_Load.

Paxton answered 20/1, 2010 at 15:8 Comment(3)
Where should this go? I tried putting it in Page_Load and that doesn't work. Thanks. EDIT: Just saw your edit. In the main page, I now have Master.pageTitle="xxx"; and in the MasterPage, I have Page.Title=pageTitle, but that doesn't work.Territus
It is the Page_Load of the MASTER page. If you need a different title for each page, then you'll have to call into a function defined in the Master page object. There is a separate trick to this but its just bookkeeping so let me know if you need help with it.Paxton
I think I misunderstood your problem a bit. I thought that you wanted to set it at the Master page level - but dynamically. It is a trivial problem if setting it at the Page level but you don't do it in HTML because the Master page should have the only <head> tags. Instead, you do it in the Page_Load.Paxton
F
0

If in your master page the title section looks like this:
<title><%=someTitleVariable%></title>

then use this as your code instead:
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> <%=pageTitle%> </asp:Content>

Franconia answered 20/1, 2010 at 15:21 Comment(0)
L
0

In defining a <asp:Content ContentPlaceHolderID="head"> control you're not modifying the title which is already there, but instead you're adding more markup to the Content Place Holder with ID "head". In your Master Page, I'd imagine you have something like this:

<head>
  <title>Master Page Title</title>
  <asp:ContentPlaceHolder id="head" runat="Server" />
</head>

So the <asp:ContentPlaceHolder> gets replaced with the <asp:Content ContentPlaceHolderID="head"> so you end up with a second <title> element.

So either remove the <title> from your Master page - which means you'll need the <title> in all your aspx files - or use some code to do something like this...

this.Title = pageTitle;
// if that doesn't do it try
// this.Header.Title = pageTitle;
Leeward answered 20/1, 2010 at 15:26 Comment(1)
Interestingly, I did not have <title> in my MasterPage, but it was generating that tag twice nevertheless.Territus
M
0

Some 2 years later this issue still persists. Hitting SEO of sites.

Using MVC 4.5RC. All you need to do is place an empty tag before master content area. There is no need to recode, set title inside the code. Like so:

<title></title>
<asp:contentPlaceHolder><title>Rise Sir...</title><asp:contentPlaceHolder>

Simple.

Mazonson answered 2/7, 2012 at 22:4 Comment(1)
Are you leaving out a </ in that second <asp:contentPlaceHolder>?Arawakan
I
0

I think using:

If you want to set title at page level

<%@ Master ... %>
<html>
<head runat="server">
  <title>
    <asp:ContentPlaceHolder ID="titleContent" runat="server" />
  </title>
</head>

Or,

If you want to set dynamic title at Master Page level.

<%@ Master ... %>
<html>
<head runat="server">
  <title>
    <asp:Literal ID="litPageTitle" runat="server"></asp:Literal>
  </title>
</head>

is better way to make sure that empty second title tag is not generated.

Inhumane answered 3/5, 2013 at 4:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.