从母版页中检索页面标题
为了管理我的页面上的页面标题,我有一个在标题中带有 ContentPlaceHolder 的母版页。
<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.Title
放在 OnInit
方法中来修改页面上的代码页面。
我需要访问母版页代码隐藏中的页面标题,但当我使用 Page.Title
时,我总是得到一个空标题。
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
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过使用
<%Page.Title = "My page title";%>
,您隐式告诉 ASP.NET 执行此在页面呈现阶段嵌入代码块。这是什么意思?在页面渲染阶段之前您将无法获取此值。假设您尝试比渲染期间早一点获取该值。这就是为什么你得到空字符串。
解决方法可以是在页面开头设置
<%@ Page
指令的Title
属性,例如:通过设置此属性,您将能够访问母版页中的
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.: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