문제

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.

도움이 되었습니까?

해결책

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";
}

다른 팁

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.

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.

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.

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>

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;

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.

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top