如何从ASP.NET 3.5中的Web内容页面设置页面标题

时间:2010-02-19 00:38:00

标签: asp.net webforms master-pages title

我已经阅读了很多有关如何执行此操作的帖子/文章,但我仍然没有从内容页面获取页面标题集。我的页面渲染正常,但我无法从内容页面获取标题集(所有页面都根据母版页设置了标题)。这是我的母版页的代码隐藏:

Partial Class zSEO
Inherits System.Web.UI.MasterPage
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Page.Header.Title = "Dynamically set in Master page"
    End Sub
End Class

以下是母版页的其余部分:

<%@ Master Language="VB" 
EnableTheming="true"
Inherits="zSEO" 
CodeFile="zSEO.master.vb" %>
<!DOCTYPE html 
 PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml" >
     <head id="Head1" runat="server">
         <title></title>
     </head>
 <body>
 <form id="form1" runat="server">    

 <div id="container">
     <div id="content">
         <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
         </asp:contentplaceholder>
     </div>    
 </div>      
 </form>
 </body>
</html>

然而,我想在网页内容页面中确定页面的价值,并将其放在我的测试内容页面中:

Public Partial Class zShowAd
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    Page.Header.Title = "Dynamically set TITLE value in the content(child) page"
End Sub

End Class

奇怪的是,我无法让调试器在内容页面的上一行停止 - 仅在主页面的相应行上。显然,我对此感到困惑。

我已经读过有其他方法可以做到这一点,但这似乎可以从我在Scott Mitchell的教程中阅读:Dynamically setting the Page Title in ASP.NET。具体来说,我试图从文章中遵循这一点: “此外,如果您使用的是母版页,则此代码可以在母版页或使用母版页的ASP.NET页面上工作。在这种情况下,应该在主页面,但ASP.NET页面仍然可以通过Page.Header访问它。“

7 个答案:

答案 0 :(得分:10)

所以需要发生的是这个

<强> MasterPage.Master

Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
    Me.Page.Title = "Dynamically set in Master page"
End Sub

<强> Default.aspx的

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Me.Page.Title = "Dynamically set in ASPX page"
End Sub

这样您的母版页标题就会在您的内容页面标题之前设置。如果您没有从内容页面设置标题,则母版页将是默认标题。如果您确实从内容页面设置了标题,那么它将覆盖它。

答案 1 :(得分:6)

问题是页面中的Page_Load方法在页面中用户控件的Page_Load方法之前运行,而母版页实际上是用户控件。

您可以在母版页中使用Page_Init方法。

答案 2 :(得分:3)

您必须记住,MasterPage是页面的子控件,因此OnLoad事件会在页面的OnLoad事件之后触发

在您的场景/示例中,页面将设置标题,然后母版页会再次设置它。要么在生命周期的后期设置它,要么围绕谁设置它来包装更多的逻辑呢?

斯科特·艾伦(Scott Allen)专门为Master Page撰写了一篇很好的文章give it a quick read to get a feel for the lifecycle order

答案 3 :(得分:2)

我也有这个问题。 我无法编辑主文件(太多可能的副作用),所以我使用了PreRender()方法的页面,它在主页面之后触发 Page_Load()

protected void Page_PreRender(object sender, EventArgs e)
{
    Page.Title = Page.Title + " - server error 500";
}

答案 4 :(得分:2)

母版页中的更简单的解决方案&lt;%:Page.Title%&gt; - 主标题就在这里

在内容页面的第一行&lt;%@ Page Title =“你的标题”语言=“C#”MasterPageFile =“〜/ _masterpages / ... etc

答案 5 :(得分:0)

我有时使用的另一个解决方案是在主页面上的标题标记之间放置一个contentplaceholder,然后您可以在内容标记中使用标签控件并将其呈现给它。

这样,您可以在控件发回后为页面提供标题。

答案 6 :(得分:0)

将Page 标题与默认主页标题相结合,您可以使用默认ASP.NET网络应用模板使用的标准模板。

<head runat="server">
    <title > <%: Page.Title %> | Portal Main site Name </title>

这个方式这个页面。标题是单个页面阅读

<%@ Page Title="Virtual Machines" ...>