To manage the page title on my pages, I have a master page with a ContentPlaceHolder in the header.

<head runat="server">
    <asp:ContentPlaceHolder runat="server" ID="headContent">
    </asp:ContentPlaceHolder>
</head>

On each of my pages, I add the meta tags and page title as follow:

<asp:content id="Header" contentplaceholderid="headContent" runat="server">
    <meta name="keywords" content="keyword1, keyword2" />   
    <meta name="description" content="page description" />
    <%Page.Title = "My page title";%>  
</asp:content>

I cannot modify the code on the pages by placing the Page.Title in the OnInit method of the page.

I need access to the page title in the codebehind of the master page, but I always get an empty title when I use Page.Title.

有帮助吗?

解决方案

By using <%Page.Title = "My page title";%> you implicitly tell the ASP.NET to execute this embedded code block during the page's render phase.

What does it mean? You won't be able to get this value before the page's render phase. Assuming you're trying to get this value a little bit earlier than during the render. That's why you get the empty string.

The workaround could be in setting your Title property of the <%@ Page directive in the beginning of your page e.g.:

<%@ Page Title="My Title Goes Here" Language="C#" ... %>

By setting this you'll be able to access the Page.Title property from your master page a little bit earlier than the page's rendering occurs.

其他提示

Just use

<title>My page title</title>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top