25

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

  1. We have a Title attribute in the page directive:

      <%@ Page Language="C#" Inherits="_Default"  Title="My Title" ......%>
    
  2. We also have <title>tag in the page:

    <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 ?

Iwona Kubowicz
  • 364
  • 3
  • 15
Mohammad Arshad Alam
  • 9,694
  • 6
  • 38
  • 61
  • Did you read the [ASP.NET Documentation on Title](http://www.asp.net/web-forms/tutorials/master-pages/specifying-the-title-meta-tags-and-other-html-headers-in-the-master-page-cs)? – xdumaine Oct 07 '13 at 12:13

3 Answers3

14

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..

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..

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.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
walther
  • 13,466
  • 5
  • 41
  • 67
  • This site is not about "Voting" ("My vote" as you say). you should provide an answer based on facts/sources/code/docs. – Royi Namir Oct 07 '13 at 12:31
  • 8
    @RoyiNamir, you know that there's no definitive answer to this question. Both options work ok. I've described both variants and said, what I prefer. What's bad about it? You really want to play with words? – walther Oct 07 '13 at 12:35
  • _And i have found the page directive overrides the html title._.... The answer should explain why. ( that's what the op asked actually) – Royi Namir Oct 07 '13 at 12:36
  • 1
    Dear Sir. Maybe you should re-read the question. It's quite clear: `So Which one should we use and why ?` I've said which one and I've said why. If you have any issues with that, feel free to downvote, report, whatever. Or maybe provide an answer yourself so you show us how wrong we are. – walther Oct 07 '13 at 12:39
  • I don't downvote just like that.Also I don't have a answer to that question. but certainly I wont say : "my vote" (unless i have a proof or something to based on my answer.). if you'll wait enough , someone will come with a "source code" answer ( via reflector or something). – Royi Namir Oct 07 '13 at 12:41
  • 4
    If you want to talk, we have a chat and many eager individuals here. Feel free to use it if you're bored, because this leads nowhere. Nothing constructive is coming from you nor you're willing to read properly. – walther Oct 07 '13 at 12:44
12

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 :

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="Exampales_test" Title="ppppppppp" %>

I also set via 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 :

enter image description here

Now let's see :

When I press Run :

The Page does have the html title value (ffffff)

enter image description here

Also at the end of PreInit

enter image description here

Investigating the OnInit shows it has already changed (in the way {if you want to know exactly in what stage - this can be done}) to ppppppppp

enter image description here

And of course the last event ( among what we've talked about) is the page_load : which does change the value :

enter image description here

And the value is :

enter image description here

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.

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).

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.

Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • 2
    It doesn't matter "which occurs at the most end", but only which one you actually use. This is a really flawed opinion. All you've shown is the sequence of processing an asp.net page with no real value (unless you're dealing with a schizophrenic who uses all approaches in one app). One unified way of setting the title nullifies your "conclusion"... – walther Oct 07 '13 at 13:08
  • @walther it's the same thing. if you use only one place , then there is no code which later override the values. my test shows how the value changes at the page life cycle from the start. now the OP should know that if needs dynamic title , then it should be set via code at a later stage and not using the HTML markup title tag. – Royi Namir Oct 07 '13 at 13:09
0

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.

Copied from here https://web.archive.org/web/20210422200927/https://www.4guysfromrolla.com/articles/051006-1.aspx

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
DotNetRussell
  • 9,716
  • 10
  • 56
  • 111