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="My 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";
而且我发现页面指令覆盖了 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 标签
基础教程推荐
- WPF 模态进度窗口 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
