使用母版页在 ASP.NET 中通过 @ Page 指令设置页面标题

发布于 2024-07-13 02:50:46 字数 1676 浏览 7 评论 0原文

我正在使用母版页,但在从 @ Page 指令设置页面标题时遇到问题。 我的所有类都继承自类 myPage,而该类又继承自 ASP.NET System.Web.UI.Page 类。 请注意:我在母版页的 head 标记中设置了 runat="server"

这是我的文件 test.aspx.vb 的 @ Page 指令的样子:

<%@ Page language="VB" MasterPageFile="~/MainMaster.master" 
autoeventwireup="false" CodeFile="test.aspx.vb" 
Inherits="test" Title="test" %>

test.aspx.vb 的样子:

Partial Class test
    Inherits myPage

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

End Class

这是我的主文件 >MainMaster.master,看起来像:

<%@ Master Language="VB" CodeFile="MainMaster.master.vb" Inherits="MainMaster" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">
    <title>untitled</title>
</head>
...

现在,当您在浏览器中查看test.aspx时,您会期望看到标题“test”。 但您会根据母版页看到“无标题”。 通过反复试验,我修改了 test 类,直接从 System.Web.UI.Page 继承,而不是像这样的 myPage

Partial Class test
    Inherits Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

End Class

:一切都很好。 为什么我的页面是 myPage 而不是 System.Web.UI.Page 的子页面会阻止在 @ Page 指令中正确设置标题?

我意识到我可以通过每个页面中的 Page_Load 方法以编程方式设置页面标题,但我宁愿在 .aspx 文件中的 @ Page 指令中执行此操作。

这是一个非常奇怪和令人沮丧的问题,我不知所措!

谢谢!!!

I am using master pages and am having trouble setting page titles from the @ Page directive. All of my classes inherit from a class myPage which inherits from the ASP.NET System.Web.UI.Page class. Please Note: I have runat="server" set in my master page's head tag.

Here's what my @ Page directives look like for the file test.aspx.vb:

<%@ Page language="VB" MasterPageFile="~/MainMaster.master" 
autoeventwireup="false" CodeFile="test.aspx.vb" 
Inherits="test" Title="test" %>

Here's what test.aspx.vb looks like:

Partial Class test
    Inherits myPage

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

End Class

This is what my master file, MainMaster.master, looks like:

<%@ Master Language="VB" CodeFile="MainMaster.master.vb" Inherits="MainMaster" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">
    <title>untitled</title>
</head>
...

Now, when you go to view test.aspx in a browser you'd expect to see the title 'test'. but instead you will see 'untitled' as per the master page. Through trial and error, I modified the test class to inherit from System.Web.UI.Page directly, instead of myPage like this:

Partial Class test
    Inherits Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

End Class

and everything worked just fine. Why would my pages being children of myPage instead of System.Web.UI.Page prevent the title from being set correctly in the @ Page directive?

I realize that I could just set the page titles programmatically through the Page_Load methods in every page, but I'd rather do it in the @ Page directives in the .aspx files.

This is a very weird and frustrating problem, and I'm at a loss!

Thanks!!!

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

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

发布评论

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

评论(3

总攻大人 2024-07-20 02:50:46

我感谢大家的帮助; 我找到了解决办法。 问题在于 myPage 类具有 Title 属性,但在该属性的 Set 部分中,并未将更改传递给 Page.Title,因为它应该是这样。

一行更改解决了我的问题:)

I thank everyone for their help; I have found a solution. The problem was that the myPage class had a property for Title, but in the Set part of the property was not passing the changes on to Page.Title as it should have been.

A one line change fixed my problem :)

闻呓 2024-07-20 02:50:46

您的基页 (myPage.vb) 中有哪些方法。

如果您要覆盖任何默认方法,您是否会调用这些页面的基本版本?

在 C# 中,我会有这样的内容:

protected override void OnInit(EventArgs e)
{
    // Do my custom processing.

    // Don't forget to call base OnInit here:
    base.OnInit(e);
}

如果您不调用这些方法,那么它们中为您发生的事件(例如连接母版页的标题)将不会触发。

What methods do you have in your base page (myPage.vb).

If you are overriding any of the default methods, are you calling the base versions of those pages?

in C# I'd have something like this:

protected override void OnInit(EventArgs e)
{
    // Do my custom processing.

    // Don't forget to call base OnInit here:
    base.OnInit(e);
}

If you don't call these methods, then the events that happen in them for you (like wiring up the masterpage's title) won't fire.

浅唱々樱花落 2024-07-20 02:50:46

我有一个和你非常相似的设置。 我的内容页面继承自自定义基页面,而自定义基页面本身又继承自页面。 我对在 aspx 上设置标题并在浏览器中显示没有任何问题。 我看到我的代码和你的代码之间的唯一区别是我的母版页具有 autoeventwireup 属性,而你的母版页没有,而且你的母版页有一个名为 codefile 的属性,而我的母版页有代码隐藏。

内容页:

<%@ Page Title="Login to Application X" Language="vb" AutoEventWireup="false" MasterPageFile="~/masterpages/mymasterpage.Master"
    CodeBehind="login.aspx.vb" Inherits=".login" %>

母版页:

<%@ Master Language="VB" AutoEventWireup="false" CodeBehind="mymasterpage.master.vb"
    Inherits=".mymasterpage" %>

I have a very similar set up to you. I have content pages inheriting from a custom base page which is itself inheriting from page. I am not having any issues with the title being set on the aspx and being shown in the browser. The only difference I see between my code and yours is my master page has the autoeventwireup property where your master page does not, and also your master page has a property called codefile and mine has codebehind.

Content Page:

<%@ Page Title="Login to Application X" Language="vb" AutoEventWireup="false" MasterPageFile="~/masterpages/mymasterpage.Master"
    CodeBehind="login.aspx.vb" Inherits=".login" %>

Master Page:

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