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?
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#" ... %>
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" %>
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.
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";
...
}
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With