マスターページからページタイトルを取得します
-
14-11-2019 - |
質問
マイページでページタイトルを管理するには、ヘッダーにContentPlayホルバを持つマスターページがあります。
<head runat="server">
<asp:ContentPlaceHolder runat="server" ID="headContent">
</asp:ContentPlaceHolder>
</head>
.
マイページのそれぞれで、メタタグとページタイトルを次のように追加します。
<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>
.
PageのPage.Title
メソッドにOnInit
を配置して、ページ上のコードを変更できません。
マスターページのCodeBehindのページタイトルにアクセスする必要がありますが、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>