Page.Title is blank in MasterPage after setting in ASPX page

By · June 8, 2016 · 0 comments

I came across an issue where I was setting Page.Title in my ASPX page's Page_Load event, but it was appearing blank when I was attempting to retrieve the value in the MasterPage's Page_PreRender event.

Thanks to a comment on this blog post, I found the solution.

Here's what the anonymous comment said:

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;

So the solution is to use the following instead of Page.Title:

(Page.Header != null ? Page.Header.Title : null) ?? Page.Title;

Read more...
ASP.NET Web Forms

Get the latest posts delivered to your inbox.

Comments

There are no comments yet. Be the first to leave a comment!

Leave a Comment

All comments are moderated and rel="nofollow" is in use. Avatars are sourced from gravatar.com – a globally recognised avatar.

Type the numbers from the picture above

About me
John Avis ...mostly about web development and programming, with a little bit of anything else related to the Internet, computers and technology.

profile for John at Stack Overflow, Q&A for professional and enthusiast programmers
Subscribe

Get the latest posts delivered to your inbox. *