asp.net page titles are blank

articles:

asp.net page titles are blank

Recently encountered this rather strange phenomenon: when using master pages and setting the page title from the aspx the title was was appearing blank.

Did several different tests with the title being set in the on Page_Load event and in the Page_PreRender. Tried both Me.Page.Title and Me.Title, but none of those things made any difference.

Having stepped away from the code behind for a moment i noticed that the title attribute in the page directive was blank i.e. Title="", could this somehow be overriding the title being set programmatically? Simple to test this idea - put some text in the Title attribute and see what happens. Lo and behold the Title being set in the code behind is now showing. Next i removed the Title attribute from the page directive entirely - and again the Title being set in the code behind was showing.

So it looks like if you are using master pages you can't have and empty Title attribute in the page directive of an aspx, interestingly though if you aren't using a master page you can have an empty Title attribute in the page directive and still set the title programmatically.

Comments

Posted on Sunday, July 1, 2012 5:25:00 PM GMT by Thanks
I ran into this problem as well. Looked in Reflector and figured out why. Seems like this is a bug to me. On the setter, if the page has a Header control, then setting the Title property actually changes Header.Title, but if the _titleToBeSet property already has a value (mine does because I'm using localized resources) then it doesn't clear out the old value and the getter still returns _titleToBeSet instead of Header.Title.

So I just changed my code to return this:
return (Page.Header != null ? Page.Header.Title : null) ?? Page.Title;

If you are using resources or for some other reason can't remove the default title from your @Page directive, this should allow you to get to the updated value that you set via the code.
Posted on Friday, May 10, 2013 1:16:00 PM GMT by Ananda Asir
Thanks. It is really useful.

Post a comment

name:
(optional)

email:
(optional)

comment: