使用 ASP.NET MasterPage 复制标题标签

发布于 2024-08-18 04:09:06 字数 1019 浏览 6 评论 0原文

我需要动态设置页面的标题,因此我使用类似于以下的代码:

<%@ 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>

但这会生成重复的标题标签。有什么办法可以解决这个问题吗?谢谢。

编辑:根据下面的建议,我现在在我的 MasterPage 中有以下内容:

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

在我的主页中有以下内容:

    this.Title="xxx";

但我没有得到任何标题(既不是“默认标题”也不是“xxx”)。

编辑:没关系。使用该方法就可以工作了。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

怪我入戏太深 2024-08-25 04:09:06

如果第二个标题标记为空并且位于 head 标记的末尾,则发生的情况是 Head 控件(注意 runat="server")看不到一个标题控件位于其控件内,因此在末尾添加一个空的标题标记。

这里的大多数答案都是有效的,但是为了进行最小的改变,您可以做的是 将以下内容添加到您的头部控件 -

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

其他选项包括 -

  • runat="server" 从头部控件上取下,但是这将意味着您无法使用服务器端控件
  • 使用标题控件并以编程方式设置它,就像您的解决方案一样。

我喜欢隐藏的标题标签,因为它允许您使用 ContentPlaceholder 方法,将其保留在模板中。它还允许您使用自定义控件来获取标题(在我们的例子中,我们使用第三方库从数据库中提取标题)

请参阅 Phil Haack 的这篇博文了解相关背景信息。

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.

掌心的温暖 2024-08-25 04:09:06

.master 的标头需要如下所示:

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

然后在 Page_Load 的页面代码隐藏中编写:

protected void Page_Load(object sender, EventArgs e)
{
    this.Title = "My Page Title";
}

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";
}
指尖凝香 2024-08-25 04:09:06

为了避免重复标签,只需执行以下操作,无需额外代码。

在您的 MasterPage 中,有类似这样的内容:

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

在每个其他页面上,将 Title="Your Title" 属性添加到页面指令中:

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

这会消除重复的标记,并将标题放在代码视图顶部清晰可见的位置。

希望这有帮助。

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.

乄_柒ぐ汐 2024-08-25 04:09:06

请尝试此操作

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

此代码位于母版页的 Page_Load 中。我必须使用强制转换(“MyPageClass”),因为我派生了自己的 PageClass,以便可以将强类型会话对象放在那里。如果你不这样做,那么你就不需要演员阵容。

如果您需要在页面级别执行此操作,我认为您可以

     Title = "Your Page Title"

在 Page_Load 中使用:

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.

如果没有你 2024-08-25 04:09:06

如果您的主页中的标题部分如下所示:
<%=someTitleVariable%>

然后将其用作您的代码:

<%=页面标题%>

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>

謌踐踏愛綪 2024-08-25 04:09:06

在定义 控件时,您不会修改已有的标题,而是使用 ID 将更多标记添加到内容占位符“头”。在您的母版页中,我想您有这样的内容:

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

因此 被替换为 < /code> 所以你最终会得到第二个 </code> 元素。

因此,要么从母版页中删除 </code> - 这意味着您的所有 aspx 文件中都需要 <code><title></code> - 或者使用一些代码来做这样的事情......

this.Title = pageTitle;
// if that doesn't do it try
// this.Header.Title = pageTitle;

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;
方圜几里 2024-08-25 04:09:06

我认为使用:

如果您想在页面级别设置标题

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

,或者,

如果您想在母版页级别设置动态标题。

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

是确保不生成空的第二个标题标签的更好方法。

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.

诗酒趁年少 2024-08-25 04:09:06

大约两年后,这个问题仍然存在。打击网站的搜索引擎优化。

使用 MVC 4.5RC。您所需要做的就是在主内容区域之前放置一个空标签。无需重新编码,在代码中设置标题。就像这样:

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

简单。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文