質問

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