使用母版页从ASP.NET中的@ Page指令设置页面标题

时间:2022-12-04 23:35:17

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.

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

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

这是我的@Page指令对于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:

这是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

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

这是我的主文件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>
...

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:

现在,当您在浏览器中查看test.aspx时,您希望看到标题为“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

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?

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

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.

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

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

这是一个非常奇怪和令人沮丧的问题,我很茫然!

Thanks!!!

3 个解决方案

#1


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.

我感谢大家的帮助;我找到了解决方案。问题是myPage类有一个Title属性,但在属性的Set部分没有将更改传递给Page.Title,因为它应该是。

A one line change fixed my problem :)

一线修改我的问题:)

#2


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

您的基页(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:

在C#我会有这样的事情:

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.

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

#3


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.

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

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

#1


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.

我感谢大家的帮助;我找到了解决方案。问题是myPage类有一个Title属性,但在属性的Set部分没有将更改传递给Page.Title,因为它应该是。

A one line change fixed my problem :)

一线修改我的问题:)

#2


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

您的基页(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:

在C#我会有这样的事情:

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.

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

#3


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.

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

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