Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Page title doesn't work

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?

like image 970
Can Poyrazoğlu Avatar asked Aug 23 '11 21:08

Can Poyrazoğlu


5 Answers

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#" ... %>
like image 81
Matthieu Charbonnier Avatar answered Nov 13 '22 19:11

Matthieu Charbonnier


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" %>
like image 28
Steven V Avatar answered Nov 13 '22 20:11

Steven V


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.

like image 33
toddb Avatar answered Nov 13 '22 21:11

toddb


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";
        ...
    }
like image 41
Igor Turman Avatar answered Nov 13 '22 20:11

Igor Turman


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
like image 29
leqid Avatar answered Nov 13 '22 21:11

leqid