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

发布于 2024-08-22 07:02:32 字数 1850 浏览 3 评论 0原文

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

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>

然而,我想要在 Web 内容页面中建立该页面的值,并且我已将其放置在我的测试内容页面中:

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

< strong>奇怪的是,我无法让调试器停止在内容页面的上面一行上 - 只能停止在母版页中的相应行上。显然,我对此感到困惑。

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

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. "

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

你是我的挚爱i 2024-08-29 07:02:32

因此,需要发生的是

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

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

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.

攒一口袋星星 2024-08-29 07:02:32

问题是页面中的Page_Load方法先于页面中用户控件中的Page_Load方法运行,而母版页实际上是一个用户控件。

您可以改用母版页中的 Page_Init 方法。

The problem is that the Page_Load method in the page runs before the Page_Load method in the user controls in the page, and a master page is actually a user control.

You can use the Page_Init method in the master page instead.

痴意少年 2024-08-29 07:02:32

母版页中更简单的解决方案 <%: Page.Title %> - 主标题位于

内容页面第一行 <%@ Page Title="Your Title" Language="C#" MasterPageFile="~/_masterpages/... 等

more simple solution in Master page <%: Page.Title %> - the main title goes here

in content page first line of it <%@ Page Title="Your Title" Language="C#" MasterPageFile="~/_masterpages/... etc

另类 2024-08-29 07:02:32

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

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

Scott Allen 有一篇专门针对母版页的好文章,快速阅读一下以了解生命周期顺序

You have to remember that the MasterPage is a child control of the Page, so the OnLoad event fires after the Page's OnLoad event.

In your scenario/example, the page would set the title, then the masterpage would set it again afterwards. Either set it later in the lifecycle or wrap some more logic around who sets it perhaps?

Scott Allen has a good article on this for Master Page's specifically, give it a quick read to get a feel for the lifecycle order.

吹泡泡o 2024-08-29 07:02:32

我也遇到了这个问题。
我无法编辑母版文件(可能的副作用太多),因此我使用了页面 PreRender() 方法,该方法在母版页 Page_Load() 之后触发

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

I was having this problem as well.
I can't edit the master file (too many possible side effects), so I used the pages PreRender() method, which fires after the master pages Page_Load()

protected void Page_PreRender(object sender, EventArgs e)
{
    Page.Title = Page.Title + " - server error 500";
}
触ぅ动初心 2024-08-29 07:02:32

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

例如,这样您就可以在控件回发后为页面指定标题。

another solution i used sometimes is to put a contentplaceholder in between the title tags on the master page, then you could use a label control in content tag and render it to that.

that way you can give the page a title after controls have posted back, for instance.

梦毁影碎の 2024-08-29 07:02:32

要将页面标题与默认母版页标题结合起来,您可以使用默认 ASP.NET Web 应用程序模板使用的标准模板。

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

这样这个页面。标题是从各个页面读取的

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

to combine Page Title with your Default MasterPage Title you can use the standard template which default ASP.NET web app template uses.

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

this ways this page.Title is read form individual pages

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