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

后端 未结 7 1464
长情又很酷
长情又很酷 2021-02-15 07:52

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 th

相关标签:
7条回答
  • 2021-02-15 08:25

    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

    0 讨论(0)
  • 2021-02-15 08:28

    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.

    0 讨论(0)
  • 2021-02-15 08:30

    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.

    0 讨论(0)
  • 2021-02-15 08:35

    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.

    0 讨论(0)
  • 2021-02-15 08:36

    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.

    0 讨论(0)
  • 2021-02-15 08:43

    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" ...>
    
    0 讨论(0)
提交回复
热议问题