25

I've got a strange problem that setting the Title property of my ASP.NET page does not have any effect, in code level. It doesn't throw an exception either. My class is a derived class of Page class but I am not overriding anything about title.

In the code I have this line:

Title = "About";

While debugging, I'm at that line, I put my cursor over Title as regular, and it displays "" an empty string, which is expected, I step down that line, expecting (obviously) Title to have the value "About" but when I hover, I still get an empty string. Property setting doesn't work. And yes, it is empty in output page too. Well, am I missing something there?

John Saunders
  • 157,405
  • 24
  • 229
  • 388
Can Poyrazoğlu
  • 29,145
  • 40
  • 152
  • 327
  • Are you setting the title as blank in the master page or the actual page? try setting the title after the page load event. – Dustin Davis Aug 23 '11 at 21:12
  • i am in a content page, and i tried to override Render method and tried to set there, but nothing changed, it seems to get set but it's not. – Can Poyrazoğlu Aug 23 '11 at 21:15
  • What do you have in the HTML? Do you have tags? does you rcontent page use a master page? – Dustin Davis Aug 23 '11 at 21:29
  • yes, I've got the Page directive on top just like any other page, and inheriting from my masterpage. i don't have an extra title tag, I just have the title directive and regular title at master page `some text` but some text isn't appearing in code either... – Can Poyrazoğlu Aug 23 '11 at 21:44
  • master.page.title wont work ? – David Aug 23 '11 at 21:47

10 Answers10

44

If you want to set the Title from C# code, make sure you don't set a title in the aspx page. (even a blank title will override the Title from the C# code)

This following code will override the Title set in C# code by an empty string:

<%@ Page Language="C#" Title="" ... %>

You have to remove the Title property to be able to set it in C# code:

<%@ Page Language="C#" ... %>
Matthieu Charbonnier
  • 2,393
  • 21
  • 29
  • 2
    Easy fix - and should be the correct answer. Not sure when this changed, but used to work without any changes 'out of the box' with VS2010 and .NET 4.0 but failed to work with VS2013 and .NET 4.5x – gchq Nov 14 '14 at 20:49
  • 2
    This should be the correct answer and not the 'hack' that is currently marked as answer.... – Nuno Agapito Mar 02 '15 at 12:38
  • Wow- first time I see two completely opposite answers (see Steven V) that both got upvoted as correct.. hehehe.. doh!?! – Piotr Kula Aug 26 '15 at 12:02
  • 1
    Thanks. Seems I am 5 years late to this party but I'm still using web forms! Thank you for helping me to stop hitting my head against the brick wall this morning! – CResults Sep 28 '20 at 10:32
9

I had a similar issue with with the Title property. Mine problem came back to the <%@ Page %> directive missing the Title property. Make sure you've added the Title property to the Page directive on the ASPX file like:

<%@ Page Language="C#" Title="Default Title" %>
Steven V
  • 15,061
  • 3
  • 56
  • 73
  • it is there. all the title tags and properties are in the right place. – Can Poyrazoğlu Aug 24 '11 at 16:56
  • 1
    Missing Title property does not cause problems. I tested it. But, with the user's case (really strange) - anything is possible :) – Igor Turman Aug 24 '11 at 16:56
  • the only thing i am concerned about is that my page is inherited. it SHOULD work, as all it does is just add an extra method which has nothing to do with Title.. – Can Poyrazoğlu Aug 24 '11 at 16:58
  • 2
    @can poyrazoğlu: you mentioned "inheriting from my masterpage". Actually, content page DOES NOT inherit from master page. It uses it but does not inherit from – Igor Turman Aug 24 '11 at 17:02
  • my bad. I wrote it wrong there, I was meaning regular "using Masterpage", *logically inheriting*, not from the OOP programming perspective. anyway, I inherit from the regular Page class. – Can Poyrazoğlu Aug 24 '11 at 17:15
  • 5
    I found that having the `Title="Default Title"` attribute in the page declaration was causing this problem for me. I removed that attribute and I was then able to set the page title from the code behind. – Albert Bori Jan 10 '13 at 19:05
  • 5
    I had the same problem when the title was black (Title="") in the page directive. *Removing* the title attribute altogether or setting it to something like "default" allowed me to change it in the code. – Michael Khalili Jun 05 '13 at 02:37
7

I was switching over to a new Master Page for my pages and my TITLES stopped working.

My old, working Master Page had this

<head runat="server">

My new, failing Master Page had this

<head>

So it was as simple as making sure the tag had runat="server" in it.

toddb
  • 501
  • 1
  • 5
  • 8
4

How about this (kind of odd but still :)):

Step 1: Add ContentPlaceHolder to the master page's title tag

...
<title>
    <asp:ContentPlaceHolder ID="TitleContentPlaceHolder" runat="server"></asp:ContentPlaceHolder>
</title>
...

Step 2: Add the following to the content page

...
<asp:Content ContentPlaceHolderID="TitleContentPlaceHolder" runat="server" ID="TitleContent">
    <asp:Literal runat="server" ID="TitleLabel"></asp:Literal>
</asp:Content>
...

Step 3: Try setting the title (e.g. on page load)

protected void Page_Load(object sender, EventArgs e)
    {
        ...
        TitleLabel.Text = "Some title";
        ...
    }
Igor Turman
  • 2,125
  • 1
  • 21
  • 25
1

I had a similar issue (setting the Me.Title property in code-behind did not change the actual title of the rendered page).

Everything started to work as expected after I completely removed the Title attribute from the <%@ Page %> directive.

I have this in the MasterPage <head>:

<title><%= Page.Title %></title>

(This bit does not seem strictly necessary, as ASP.NET will add a <title> element to the <head> anyway... but without it, the Visual Studio HTML validator complains that "Element 'title' appears too few times" so I leave it there.)

  • Visual Studio 2010 Pro
  • .NET 4.0
  • IIS 7.0
leqid
  • 851
  • 9
  • 17
1

this work only in PreRender

protected void Page_PreRender(object sender, EventArgs e)
    {
        Page.Title = "Some title";

    }
0
<%@ Master ..
<head runat="server">
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title><%: Page.Title %></title>

..

<%@ Page Title="ABOUT" ..
tsyselsky
  • 111
  • 1
  • 4
0
<%@ Page meta:resourcekey="PageResource1" culture="auto"

You see?
Always check existence of meta:resourcekey.
It turns our that you have a .resx resource file that contains a record

PageResource1.Title

with an empty value.
If you forget about it, all you'll do is use the

protected void Page_Load(object sender, EventArgs e)
{
    Title = "My tilte";
it3xl
  • 1,279
  • 14
  • 28
0

The top answers are both right. You can either remove Title from the <%@ Page directive or make sure it is not blank (Title=""). So, if you have Title="Foo", you can change it in code. If you remove Title="" from the Page directive, you can change it in code.

Justin Tolchin
  • 216
  • 3
  • 13
-3

Try setting the title after the page DataBind:

public override void DataBind()
{
    base.DataBind(true);
    Title = "Ballout";
}
Uwe Keim
  • 36,867
  • 50
  • 163
  • 268
Alan
  • 1