asp.net中的Page.Title vs Title标签 [英] Page.Title vs Title tag in asp.net

查看:62
本文介绍了asp.net中的Page.Title vs Title标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用asp.net.我注意到我们可以通过两种方式配置页面标题(静态和动态):

I am using asp.net. I have noticed that we can configure page title (static and dynamic both) in two ways:

  1. 我们在页面指令中具有 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";
    

    我发现page指令覆盖了html标题.那么,我们应该使用哪一个?为什么呢?

    And i have found the page directive overrides the html title. So Which one should we use and why ?

    推荐答案

    最大的区别是,使用 MyTitle.Text 时,您必须用 id 装饰Title元素,并且 runat 属性,并记住它的名称,以便您可以引用它.例如,当您使用母版页时,从子页访问此值也不是那么容易.

    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来说都是通用的,因此在我看来它更通用.与您合作的每个新开发人员都无需学习任何新知识,只需使用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 vs Title标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆