How to set Page's Title from a web content page in ASP.NET 3.5
Asked Answered
U

7

10

I've read through quite a bit of posts/articles on how to do this and I still am not getting the page title set from the content page. My pages render OK except I can't get the title set from the content page (all the page's have Title set as per the master page). Here's the codebehind for my master page:

Partial Class zSEO
Inherits System.Web.UI.MasterPage
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Page.Header.Title = "Dynamically set in Master page"
    End Sub
End Class

Here is the rest of the master page:

<%@ Master Language="VB" 
EnableTheming="true"
Inherits="zSEO" 
CodeFile="zSEO.master.vb" %>
<!DOCTYPE html 
 PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml" >
     <head id="Head1" runat="server">
         <title></title>
     </head>
 <body>
 <form id="form1" runat="server">    

 <div id="container">
     <div id="content">
         <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
         </asp:contentplaceholder>
     </div>    
 </div>      
 </form>
 </body>
</html>

Yet, it is in the web content page that I want to establish the value of the for the page and I have placed this in my testing content page:

Public Partial Class zShowAd
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    Page.Header.Title = "Dynamically set TITLE value in the content(child) page"
End Sub

End Class

Strangely, I cannot get the debugger to stop on the line above in the content page - only on the corresponding line in the master page. Clearly, I am confused on this.

I've read there are other ways to do this but this seemed to be possible from what I read at Scott Mitchell's tutorial at: Dynamically setting the Page Title in ASP.NET. Specifically, I tried to follow this from the article: "Furthermore, if you are using master pages, this code can work, as written, from either the master page or the ASP.NET page that uses the master page. In such a scenario, the region should be defined in the master page, but the ASP.NET page can still access it via Page.Header. "

Ulotrichous answered 19/2, 2010 at 0:38 Comment(0)
G
10

So what needs to happen is this

MasterPage.Master

Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
    Me.Page.Title = "Dynamically set in Master page"
End Sub

Default.aspx

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Me.Page.Title = "Dynamically set in ASPX page"
End Sub

This way your master page title is set BEFORE your content page title. If you do not set a title from the content page, the masterpage will be the default title. If you do set a title from the content page, then it will override it.

Graphic answered 19/2, 2010 at 0:58 Comment(1)
Thank you guys. I think it is a little counter-intuitive that the Page_Load of the master page would come AFTER the Page_Load of the content page but now thinking of the master page as like a user control it makes more sense. BTW, I had neglect the "Handles Me.Load" on the Page_Load in the .aspx file and as soon as I added it, then the debugger stopped there on a breakpoint. So many thanks.Ulotrichous
H
6

The problem is that the Page_Load method in the page runs before the Page_Load method in the user controls in the page, and a master page is actually a user control.

You can use the Page_Init method in the master page instead.

Hypercorrect answered 19/2, 2010 at 0:43 Comment(0)
H
4

more simple solution in Master page <%: Page.Title %> - the main title goes here

in content page first line of it <%@ Page Title="Your Title" Language="C#" MasterPageFile="~/_masterpages/... etc

Homan answered 21/9, 2015 at 2:2 Comment(0)
W
3

You have to remember that the MasterPage is a child control of the Page, so the OnLoad event fires after the Page's OnLoad event.

In your scenario/example, the page would set the title, then the masterpage would set it again afterwards. Either set it later in the lifecycle or wrap some more logic around who sets it perhaps?

Scott Allen has a good article on this for Master Page's specifically, give it a quick read to get a feel for the lifecycle order.

Whitehot answered 19/2, 2010 at 0:42 Comment(0)
E
2

I was having this problem as well. I can't edit the master file (too many possible side effects), so I used the pages PreRender() method, which fires after the master pages Page_Load()

protected void Page_PreRender(object sender, EventArgs e)
{
    Page.Title = Page.Title + " - server error 500";
}
Enthronement answered 4/2, 2014 at 0:34 Comment(0)
F
0

another solution i used sometimes is to put a contentplaceholder in between the title tags on the master page, then you could use a label control in content tag and render it to that.

that way you can give the page a title after controls have posted back, for instance.

Freese answered 19/2, 2010 at 1:3 Comment(0)
H
0

to combine Page Title with your Default MasterPage Title you can use the standard template which default ASP.NET web app template uses.

<head runat="server">
    <title > <%: Page.Title %> | Portal Main site Name </title>

this ways this page.Title is read form individual pages

<%@ Page Title="Virtual Machines" ...>
Harbinger answered 31/12, 2016 at 12:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.