How to set page title on web content page in ASP.NET 3.5

I have read quite a few posts / articles on how to do this, and I still do not get the page title indicated on the content page. My pages display OK, except that I cannot get a set of titles on the content page (the whole page has a title set on the main page). Here is the codebehind for my main 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 main 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> 

However, it is on the web content page that I want to set the value for the page, and I placed it on my test 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 

Strange, I can’t get the debugger to stop on the line above on the content page - only on the corresponding line of the main page. It is clear that I am confused by this.

I read that there are other ways to do this, but it seemed possible from what I read in Scott Mitchell's tutorial: Dynamically adjusting the page title in ASP.NET . In particular, I tried to follow this from the article: "In addition, if you use master pages, this code can work, as it is written, from the main page or ASP.NET page using the main page. In this scenario, the region should be defined on the main page, but the ASP.NET page can still access it through the PageHeader page. "

+7
webforms master-pages title
source share
7 answers

So what should happen is

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 

Thus, the title of the main page is set before the title of the content page. If you did not set a title on the content page, the default title will be the main page. If you set the title on the content page, it will override it.

+10
source share

The problem is that the Page_Load method on the page is started before the page_Load method in the user control is on the page, and the main page is the user control.

Instead, you can use the Page_Init method on the main page.

+6
source share

You must remember that MasterPage is a child of the page, so the OnLoad event OnLoad after the < OnLoad .

In your script / example, the page will set the title, after which the master page will set it again. Either install it later in the life cycle, or wrap some more logic around who can install it?

Scott Allen has a good article about this for the main page, give her a quick read to feel the order for the life cycle .

+3
source share

I also had this problem. I cannot edit the main file (there are too many possible side effects), so I used the PreRender () method, which runs after the master pages Page_Load ()

 protected void Page_PreRender(object sender, EventArgs e) { Page.Title = Page.Title + " - server error 500"; } 
+2
source share

simpler solution on the main page <%: Page.Title%> - here the title is the title

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

+2
source share

another solution that I sometimes used is to put the content owner between the title tags on the main page, then you could use the label control in the content tag and display it.

this way you can give the page a title after the controls are sent back, for example.

0
source share

To combine the Header page with your default MasterPage Header , you can use a standard template that uses the default ASP.NET web application template.

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

this this page this.Title is read on separate pages

 <%@ Page Title="Virtual Machines" ...> 
0
source share

All Articles