Reputation: 34780
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?
Upvotes: 30
Views: 18297
Reputation: 463
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.
Upvotes: 0
Reputation: 2672
<%@ 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";
Upvotes: 0
Reputation: 2982
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#" ... %>
Upvotes: 52
Reputation: 72
this work only in PreRender
protected void Page_PreRender(object sender, EventArgs e)
{
Page.Title = "Some title";
}
Upvotes: 1
Reputation: 551
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.
Upvotes: 7
Reputation: 121
<%@ 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" ..
Upvotes: 0
Reputation: 931
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.)
Upvotes: 1
Reputation: 1
Try setting the title after the page DataBind:
public override void DataBind()
{
base.DataBind(true);
Title = "Ballout";
}
Upvotes: -3
Reputation: 16585
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" %>
Upvotes: 11
Reputation: 2205
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";
...
}
Upvotes: 4