如何在 ASP.NET 3.5 中的 Web 内容页面设置页面标题
我已经阅读了大量关于如何执行此操作的帖子/文章,但我仍然没有从内容页面获取页面标题集。我的页面呈现正常,但我无法从内容页面获取标题集(所有页面都根据母版页设置了标题)。这是我的母版页的代码隐藏:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
因此,需要发生的是
MasterPage.Master
Default.aspx
这样,您的母版页标题就设置在内容页标题之前。如果您没有在内容页中设置标题,则母版页将是默认标题。如果您确实从内容页面设置了标题,那么它将覆盖它。
So what needs to happen is this
MasterPage.Master
Default.aspx
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.
问题是页面中的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.
母版页中更简单的解决方案 <%: 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
您必须记住,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'sOnLoad
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.
我也遇到了这个问题。
我无法编辑母版文件(可能的副作用太多),因此我使用了页面 PreRender() 方法,该方法在母版页 Page_Load() 之后触发
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()
我有时使用的另一个解决方案是在母版页上的标题标记之间放置一个 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.
要将页面标题与默认母版页标题结合起来,您可以使用默认 ASP.NET Web 应用程序模板使用的标准模板。
这样这个页面。标题是从各个页面读取的
to combine Page Title with your Default MasterPage Title you can use the standard template which default ASP.NET web app template uses.
this ways this page.Title is read form individual pages