Page.Title vs Title tag in asp.net(asp.net 中的 Page.Title 与 Title 标签)
问题描述
我正在使用 asp.net.我注意到我们可以通过两种方式配置页面标题(静态和动态):
I am using asp.net. I have noticed that we can configure page title (static and dynamic both) in two ways:
我们在页面指令中有一个
Title
属性:
<%@ Page Language="C#" Inherits="_Default" Title="TXkgVGl0bGU=" ......%>
我们在页面中也有
标签:
<title runat="server" id="MyTitle"> My Title</title>
两者都可以在代码隐藏文件中访问:
Both can be accessed in code-behind file:
MyTitle.Text = "Title from Code behind";
Page.Title = "UGFnZSBUaXRsZSBmcm9tIENT";
而且我发现页面指令覆盖了 html 标题.那么我们应该使用哪一个,为什么?
And i have found the page directive overrides the html title. So Which one should we use and why ?
推荐答案
最大的不同在于 MyTitle.Text
你必须用 id
AND 修饰 Title 元素runat
属性,并记住它的名称以便您可以引用它.例如,当您使用 Masterpage 时,从子页面访问此值也不是那么容易..
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..
另一方面,Page.Title
对每个页面都是通用的,所以在我看来它更通用.与您合作的每位新开发人员都无需学习任何新知识,只需使用 Page.Title 格式即可..
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
So my vote would go to the "traditional" 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.
这篇关于asp.net 中的 Page.Title 与 Title 标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:asp.net 中的 Page.Title 与 Title 标签


- C# 中多线程网络服务器的模式 2022-01-01
- 输入按键事件处理程序 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04