从母版页中检索页面标题

发布于 2024-12-29 08:01:21 字数 719 浏览 2 评论 0原文

为了管理我的页面上的页面标题,我有一个在标题中带有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遥远的绿洲 2025-01-05 08:01:21

通过使用<%Page.Title = "My page title";%>,您隐式告诉 ASP.NET 执行此在页面呈现阶段嵌入代码块

这是什么意思?在页面渲染阶段之前您将无法获取此值。假设您尝试比渲染期间早一点获取该值。这就是为什么你得到空字符串。

解决方法可以是在页面开头设置 <%@ Page 指令的 Title 属性,例如:

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

通过设置此属性,您将能够访问母版页中的 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.

苏佲洛 2025-01-05 08:01:21

只需使用

<title>My page title</title>

Just use

<title>My page title</title>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文