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

How to set Page#39;s Title from a web content page in ASP.NET 3.5(如何从 ASP.NET 3.5 中的网页内容页面设置页面标题)

本文介绍了如何从 ASP.NET 3.5 中的网页内容页面设置页面标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了很多关于如何做到这一点的帖子/文章,但我仍然没有从内容页面获得页面标题集.我的页面呈现正常,但我无法从内容页面获取标题集(所有页面的标题都按照母版页设置).这是我的母版页的代码隐藏:

部分类zSEO继承 System.Web.UI.MasterPageProtected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)Page.Header.Title = "在母版页中动态设置"结束子结束班

这是母版页的其余部分:

<%@ Master Language="VB"启用主题=真"继承=zSEO"CodeFile="zSEO.master.vb" %><!DOCTYPE htmlPUBLIC "-//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><身体><form id="form1" runat="server"><div id="容器"><div id="内容"><asp:contentplaceholder id="ContentPlaceHolder1" runat="server"></asp:contentplaceholder>

</表单>

然而,我想在网页内容页面中建立页面的值,并将其放置在我的测试内容页面中:

公共部分类 zShowAd继承 System.Web.UI.PageProtected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)Page.Header.Title = "在内容(子)页面中动态设置 TITLE 值"结束子结束班

奇怪的是,我无法让调试器停在内容页面的上面一行——只能停在母版页的相应行上.显然,我对此感到困惑.

我已经读过有其他方法可以做到这一点,但从我在 Scott Mitchell 的教程中阅读的内容来看,这似乎是可能的:在 ASP.NET 中动态设置页面标题.具体来说,我试图从文章中遵循这一点:此外,如果您使用母版页,则此代码可以按照编写的方式从母版页或使用母版页的 ASP.NET 页工作.在这种情况下,应在母版页,但 ASP.NET 页仍然可以通过 Page.Header 访问它.

解决方案

那么需要做的就是这个

MasterPage.Master

Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) 处理 Me.InitMe.Page.Title = "在母版页中动态设置"结束子

Default.aspx

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) 处理 Me.LoadMe.Page.Title = "在 ASPX 页面中动态设置"结束子

通过这种方式,您的母版页标题设置在您的内容页标题之前.如果您未从内容页面设置标题,则母版页将是默认标题.如果您确实从内容页面设置了标题,那么它将覆盖它.

I've read through quite a bit of posts/articles on how to do this and I still am not getting the page title set from the content page. My pages render OK except I can't get the title set from the content page (all the page's have Title set as per the master page). Here's the codebehind for my master page:

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

Here is the rest of the master page:

<%@ 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>

Yet, it is in the web content page that I want to establish the value of the for the page and I have placed this in my testing content page:

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

Strangely, I cannot get the debugger to stop on the line above in the content page - only on the corresponding line in the master page. Clearly, I am confused on this.

I've read there are other ways to do this but this seemed to be possible from what I read at Scott Mitchell's tutorial at: Dynamically setting the Page Title in ASP.NET. Specifically, I tried to follow this from the article: "Furthermore, if you are using master pages, this code can work, as written, from either the master page or the ASP.NET page that uses the master page. In such a scenario, the region should be defined in the master page, but the ASP.NET page can still access it via Page.Header. "

解决方案

So what needs to happen is this

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

This way your master page title is set BEFORE your content page title. If you do not set a title from the content page, the masterpage will be the default title. If you do set a title from the content page, then it will override it.

这篇关于如何从 ASP.NET 3.5 中的网页内容页面设置页面标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何从 ASP.NET 3.5 中的网页内容页面设置页面标题

基础教程推荐