C# 从母版页检索页面标题

C# 从母版页检索页面标题,c#,asp.net,master-pages,page-title,C#,Asp.net,Master Pages,Page Title,要管理页面上的页面标题,我有一个母版页,标题中有一个ContentPlaceHolder <head runat="server"> <asp:ContentPlaceHolder runat="server" ID="headContent"> </asp:ContentPlaceHolder> </head> 在我的每个页面上,我都添加了元标记和页面标题,如下所示: <asp:content id="Header"

要管理页面上的页面标题,我有一个母版页,标题中有一个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
方法中来修改页面上的代码

我需要访问母版页codebehind中的页面标题,但当我使用
page.title
时,我总是得到一个空标题

只要使用

<title>My page title</title>
我的页面标题
通过使用
可以隐式地告诉ASP.NET在页面的呈现阶段执行此操作

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


解决方法可能是设置
Title
属性。通过使用此结构,您可以设置页面的标题,但无法通过
page.Title
属性返回此值
<%@ Page Title="My Title Goes Here" Language="C#" ... %>