C# asp.net 中的 Page.Title 与 Title 标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19224443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Page.Title vs Title tag in asp.net
提问by Arshad
I am using asp.net. I have noticed that we can configure page title (static and dynamic both) in two ways:
我正在使用asp.net。我注意到我们可以通过两种方式配置页面标题(静态和动态):
We have a
Title
attribute in the page directive:<%@ Page Language="C#" Inherits="_Default" Title="My Title" ......%>
We also have
<title>
tag in the page:<title runat="server" id="MyTitle"> My Title</title>
我们
Title
在页面指令中有一个属性:<%@ Page Language="C#" Inherits="_Default" Title="My Title" ......%>
我们
<title>
在页面中也有标签:<title runat="server" id="MyTitle"> My Title</title>
Both can be accessed in code-behind file:
两者都可以在代码隐藏文件中访问:
MyTitle.Text = "Title from Code behind";
Page.Title = "Page Title from CS";
And i have found the page directive overrides the html title. So Which one should we use and why ?
我发现页面指令覆盖了 html 标题。那么我们应该使用哪一个,为什么?
采纳答案by walther
The biggest difference is that with MyTitle.Text
you have to decorate Title element with an id
AND runat
attributes, and remember it's name so you can reference it. Also accessing this value isn't that easy from child pages when you're using Masterpage for instance..
最大的区别是MyTitle.Text
你必须用id
ANDrunat
属性装饰 Title 元素,并记住它的名称以便你可以引用它。例如,当您使用 Masterpage 时,从子页面访问此值也不是那么容易。
On the other hand, Page.Title
is common to every Page, so it's more universal in my opinion. Every new developer you'll work with won't have to learn anything new, just use the Page.Title format..
另一方面,Page.Title
对于每个页面都是通用的,所以在我看来它更通用。与您合作的每个新开发人员都不必学习任何新东西,只需使用 Page.Title 格式即可。
So my vote would go to the "traditional" Page.Title
所以我的投票会投给“传统的” Page.Title
Whichever you like to use, stick with it, so you won't mix various ways of setting the title. That way you won't have to worry about which event happens first or about your colleague overwriting your values.
无论你喜欢使用哪个,坚持使用它,这样你就不会混合各种设置标题的方法。这样您就不必担心哪个事件先发生或您的同事会覆盖您的价值观。
回答by Anthony Russell
Here is a good article on the differences. They basically do the same thing. It's just WHAT you want to do that matters.
这是一篇关于差异的好文章。他们基本上做同样的事情。重要的是你想做什么。
While the
<title>
can be set statically in an ASP.NET web page, in many scenarios the title is dependent upon the data displayed in the page. For example, a website might have a ShowProduct.aspx?ID=productID page. Rather than using a static<title>
, the value of the<title>
would ideally be the name of the product being viewed (that is, the product whose ProductID equaled the productID value passed through the querystring). Unfortunately, in ASP.NET version 1.x, setting any HTML metadata elements (such as<title>
) required that the developer add a Literal control in the proper place in the HTML markup and then set its value programmatically in the ASP.NET page's code-behind class.
虽然
<title>
可以在 ASP.NET 网页中静态设置,但在许多情况下,标题取决于页面中显示的数据。例如,一个网站可能有一个 ShowProduct.aspx?ID=productID 页面。理想情况下<title>
, 的值不是使用静态的,而是<title>
正在查看的产品的名称(即 ProductID 等于通过查询字符串传递的 productID 值的产品)。不幸的是,在 ASP.NET 1.x 版中,设置任何 HTML 元数据元素(例如<title>
)需要开发人员在 HTML 标记中的适当位置添加 Literal 控件,然后在 ASP.NET 页面的代码中以编程方式设置其值 -课后。
Copied from here http://www.4guysfromrolla.com/articles/051006-1.aspx
回答by Royi Namir
Short answer : ( it depends on your needs). i'll explain.
简短回答:(这取决于您的需求)。我来解释一下。
- it depends if your title should change at runtime.
- 这取决于您的标题是否应该在运行时更改。
Long answer :
长答案:
Here is my observation(with a small test) :
这是我的观察(通过一个小测试):
I set title
at the Page directive
:
我设置title
在Page directive
:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="Exampales_test" Title="ppppppppp" %>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="Exampales_test" Title="ppppppppp" %>
I also set via html
:
我还通过html
以下方式设置:
<head runat="server" ID="hhh">
...
<title runat="server">fffffffff</title>
</head>
I have this test code :
我有这个测试代码:
protected override void OnPreInit(EventArgs e)
{
// <----- breakpoint here (A)
base.OnPreInit(e);
// <----- breakpoint here (B)
}
protected void Page_Load(object sender, EventArgs e)
{ // <----- breakpoint here (C)
this.Title = "cccccccc";
}
Visual :
视觉的 :
Now let's see :
现在让我们看看:
When I press Run
:
当我按下Run
:
The Page does have the html titlevalue (ffffff
)
该页面确实具有 html 标题值 ( ffffff
)
Also at the end of PreInit
也在年底 PreInit
Investigating the OnInit
shows it has alreadychanged (in the way {if you want to know exactly in what stage - this can be done}) to ppppppppp
调查OnInit
它已经改变的节目(以{如果你想确切地知道在哪个阶段 - 这可以完成})到 ppppppppp
And of course the last event ( among what we've talked about) is the page_load
: which does change the value :
当然,最后一个事件(我们已经讨论过的)是page_load
: 它确实改变了值:
And the value is :
价值是:
So which to choose ?
那么选择哪个呢?
If your code is changing the Title
dynamically (I mean at runtime), don't use the html markup at all nor the page directive.
如果您的代码正在Title
动态更改(我的意思是在运行时),则根本不要使用 html 标记,也不要使用 page 指令。
e.g. if your code is (for example) has ASCX and that ACSX should change the title , then just use code ( not directive nor html markup).
例如,如果您的代码(例如)具有 ASCX 并且 ACSX 应该更改标题,那么只需使用代码(不是指令也不是 html 标记)。
As you noticed already - the value which wins is The one who occurs last(timeline)
正如您已经注意到的 - 获胜的价值是最后发生的价值(时间线)
It starts with the html markup value , but then the server side code begins to activate and changes values.
它以 html 标记值开始,然后服务器端代码开始激活并更改值。