设置页面标题不起作用

时间:2022-12-04 23:39:35

I've got a strange problem that setting the Title property of my ASP.NET page does not have any effect, in code level. It doesn't throw an exception either. My class is a derived class of Page class but I am not overriding anything about title.

我有一个奇怪的问题,在代码级别设置我的ASP.NET页面的Title属性没有任何影响。它也没有抛出异常。我的类是Page类的派生类,但我没有重写关于title的任何内容。

In the code I have this line:

在代码中我有这一行:

Title = "About";

While debugging, I'm at that line, I put my cursor over Title as regular, and it displays "" an empty string, which is expected, I step down that line, expecting (obviously) Title to have the value "About" but when I hover, I still get an empty string. Property setting doesn't work. And yes, it is empty in output page too. Well, am I missing something there?

在调试的时候,我就在那条线上,我将光标放在Title上作为常规,并显示“”一个空字符串,这是预期的,我走下那条线,期待(显然)Title有值“About”但是当我盘旋时,我仍然得到一个空字符串。属性设置不起作用。是的,它在输出页面也是空的。好吧,我错过了什么吗?

8 个解决方案

#1


38  

If you want to set the Title from C# code, make sure you don't set a title in the aspx page. (even a blank title will override the Title from the C# code)

如果要从C#代码设置标题,请确保未在aspx页面中设置标题。 (即使是空白标题也会覆盖C#代码中的标题)

This following code will override the Title set in C# code by an empty string:

以下代码将用空字符串覆盖C#代码中的Title集:

<%@ Page Language="C#" Title="" ... %>

You have to remove the Title property to be able to set it in C# code:

您必须删除Title属性才能在C#代码中设置它:

<%@ Page Language="C#" ... %>

#2


9  

I had a similar issue with with the Title property. Mine problem came back to the <%@ Page %> directive missing the Title property. Make sure you've added the Title property to the Page directive on the ASPX file like:

我对Title属性也有类似的问题。我的问题回到了缺少Title属性的<%@ Page%>指令。确保已将Title属性添加到ASPX文件上的Page指令,如:

<%@ Page Language="C#" Title="Default Title" %>

#3


6  

I was switching over to a new Master Page for my pages and my TITLES stopped working.

我正在切换到我的页面的新母版页,我的标题停止了工作。

My old, working Master Page had this

我老,工作的Master Page有这个

<head runat="server">

My new, failing Master Page had this

我新的,失败的母版页有这个

<head>

So it was as simple as making sure the tag had runat="server" in it.

所以它就像确保标签中有runat =“server”一样简单。

#4


4  

How about this (kind of odd but still :)):

怎么样(有点奇怪,但仍然:)):

Step 1: Add ContentPlaceHolder to the master page's title tag

第1步:将ContentPlaceHolder添加到母版页的标题标记中

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

Step 2: Add the following to the content page

第2步:将以下内容添加到内容页面

...
<asp:Content ContentPlaceHolderID="TitleContentPlaceHolder" runat="server" ID="TitleContent">
    <asp:Literal runat="server" ID="TitleLabel"></asp:Literal>
</asp:Content>
...

Step 3: Try setting the title (e.g. on page load)

第3步:尝试设置标题(例如,在页面加载时)

protected void Page_Load(object sender, EventArgs e)
    {
        ...
        TitleLabel.Text = "Some title";
        ...
    }

#5


1  

I had a similar issue (setting the Me.Title property in code-behind did not change the actual title of the rendered page).

我遇到了类似的问题(在代码隐藏中设置Me.Title属性并没有改变渲染页面的实际标题)。

Everything started to work as expected after I completely removed the Title attribute from the <%@ Page %> directive.

在我从<%@ Page%>指令中完全删除了Title属性后,一切都按预期开始工作。

I have this in the MasterPage <head>:

我在MasterPage 中有这个:

<title><%= Page.Title %></title>

(This bit does not seem strictly necessary, as ASP.NET will add a <title> element to the <head> anyway... but without it, the Visual Studio HTML validator complains that "Element 'title' appears too few times" so I leave it there.)

(这一点似乎并不是绝对必要的,因为ASP.NET无论如何都会在中添加一个

元素......但没有它,Visual Studio HTML验证器会抱怨“元素'标题'出现的次数太少”所以我把它留在那里。)</p>
  • Visual Studio 2010 Pro
  • Visual Studio 2010专业版
  • .NET 4.0
  • .NET 4.0
  • IIS 7.0
  • IIS 7.0

#6


1  

this work only in PreRender

这项工作仅限于PreRender

protected void Page_PreRender(object sender, EventArgs e)
    {
        Page.Title = "Some title";

    }

#7


0  

<%@ Master ..
<head runat="server">
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title><%: Page.Title %></title>

..

<%@ Page Title="ABOUT" ..

#8


-3  

Try setting the title after the page DataBind:

尝试在页面DataBind之后设置标题:

public override void DataBind()
{
    base.DataBind(true);
    Title = "Ballout";
}

#1


38  

If you want to set the Title from C# code, make sure you don't set a title in the aspx page. (even a blank title will override the Title from the C# code)

如果要从C#代码设置标题,请确保未在aspx页面中设置标题。 (即使是空白标题也会覆盖C#代码中的标题)

This following code will override the Title set in C# code by an empty string:

以下代码将用空字符串覆盖C#代码中的Title集:

<%@ Page Language="C#" Title="" ... %>

You have to remove the Title property to be able to set it in C# code:

您必须删除Title属性才能在C#代码中设置它:

<%@ Page Language="C#" ... %>

#2


9  

I had a similar issue with with the Title property. Mine problem came back to the <%@ Page %> directive missing the Title property. Make sure you've added the Title property to the Page directive on the ASPX file like:

我对Title属性也有类似的问题。我的问题回到了缺少Title属性的<%@ Page%>指令。确保已将Title属性添加到ASPX文件上的Page指令,如:

<%@ Page Language="C#" Title="Default Title" %>

#3


6  

I was switching over to a new Master Page for my pages and my TITLES stopped working.

我正在切换到我的页面的新母版页,我的标题停止了工作。

My old, working Master Page had this

我老,工作的Master Page有这个

<head runat="server">

My new, failing Master Page had this

我新的,失败的母版页有这个

<head>

So it was as simple as making sure the tag had runat="server" in it.

所以它就像确保标签中有runat =“server”一样简单。

#4


4  

How about this (kind of odd but still :)):

怎么样(有点奇怪,但仍然:)):

Step 1: Add ContentPlaceHolder to the master page's title tag

第1步:将ContentPlaceHolder添加到母版页的标题标记中

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

Step 2: Add the following to the content page

第2步:将以下内容添加到内容页面

...
<asp:Content ContentPlaceHolderID="TitleContentPlaceHolder" runat="server" ID="TitleContent">
    <asp:Literal runat="server" ID="TitleLabel"></asp:Literal>
</asp:Content>
...

Step 3: Try setting the title (e.g. on page load)

第3步:尝试设置标题(例如,在页面加载时)

protected void Page_Load(object sender, EventArgs e)
    {
        ...
        TitleLabel.Text = "Some title";
        ...
    }

#5


1  

I had a similar issue (setting the Me.Title property in code-behind did not change the actual title of the rendered page).

我遇到了类似的问题(在代码隐藏中设置Me.Title属性并没有改变渲染页面的实际标题)。

Everything started to work as expected after I completely removed the Title attribute from the <%@ Page %> directive.

在我从<%@ Page%>指令中完全删除了Title属性后,一切都按预期开始工作。

I have this in the MasterPage <head>:

我在MasterPage 中有这个:

<title><%= Page.Title %></title>

(This bit does not seem strictly necessary, as ASP.NET will add a <title> element to the <head> anyway... but without it, the Visual Studio HTML validator complains that "Element 'title' appears too few times" so I leave it there.)

(这一点似乎并不是绝对必要的,因为ASP.NET无论如何都会在中添加一个

元素......但没有它,Visual Studio HTML验证器会抱怨“元素'标题'出现的次数太少”所以我把它留在那里。)</p>
  • Visual Studio 2010 Pro
  • Visual Studio 2010专业版
  • .NET 4.0
  • .NET 4.0
  • IIS 7.0
  • IIS 7.0

#6


1  

this work only in PreRender

这项工作仅限于PreRender

protected void Page_PreRender(object sender, EventArgs e)
    {
        Page.Title = "Some title";

    }

#7


0  

<%@ Master ..
<head runat="server">
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title><%: Page.Title %></title>

..

<%@ Page Title="ABOUT" ..

#8


-3  

Try setting the title after the page DataBind:

尝试在页面DataBind之后设置标题:

public override void DataBind()
{
    base.DataBind(true);
    Title = "Ballout";
}