How do I reference a master page from an ASP.NET webform? The following statement does not work:
如何从ASP.NET webform引用母版页?以下声明不起作用:
this.MasterPage.Page.Title = "My Title";
5 个解决方案
#1
In your aspx, below the Page directive, write:
在你的aspx中,在Page指令下面写:
<%@ MasterType VirtualPath="YourMasterFile" %>
And then from your code, write Master. anything that you want to use, for example:
然后从你的代码中,写下Master。你想要使用的任何东西,例如:
Master.Title = "My Title";
#2
you have to cast this.MasterPage into the type of masterpage you have, and then you can access it as you'd expect
你必须将this.MasterPage转换为你拥有的主页类型,然后你可以按照你的期望访问它
var mp = this.MasterPage as MyMasterPageType;
mp.Property = value... etc
#3
From the Page you can use the Master
property and cast this to your master page. i.e. (MyMasterPage)this.Master
. However, whenever I attempt to do this I always check it can be cast first so I normally end up with something like...
在页面中,您可以使用Master属性并将其强制转换为母版页。即(MyMasterPage)this.Master。但是,每当我尝试这样做时,我总是检查它是否可以先施放,所以我通常最终会得到类似的东西......
MyMasterPage master;
if (this.Master is MyMasterPage)
{
master = (MyMasterPage)this.Master
//do stuff with master.
}
If all you are wanting to do is change the title then you can just use Page.Title
and make sure that the head tag in your master page is set to runat='server'.
如果您想要做的就是更改标题,那么您可以使用Page.Title并确保母版页中的head标记设置为runat ='server'。
#4
In your code write :
在你的代码中写:
Dim masterpage As New MasterPage
masterpage = CType(masterpage, MasterPage)
and in your source code where the language is defined and etc type this
并在您的源代码中定义语言等,输入此内容
MasterPageFile="~/MasterPage.master"
If you write in C#
如果用C#写的话
MasterPage masterpage = new MasterPage();
masterpage = (MasterPage)masterpage;
#5
In your initial question (before it was edited) I think you mentioned "global settings". Depending on what you want to do, you may want to look into the BasePage concept as well, since I think it might be more suitable. Since you derive from it, all its members are accessible in your code-behind.
在您的初始问题(编辑之前)中,我认为您提到了“全局设置”。根据您的想法,您可能也想查看BasePage概念,因为我认为它可能更合适。由于您是从它派生的,因此可以在代码隐藏中访问其所有成员。
#1
In your aspx, below the Page directive, write:
在你的aspx中,在Page指令下面写:
<%@ MasterType VirtualPath="YourMasterFile" %>
And then from your code, write Master. anything that you want to use, for example:
然后从你的代码中,写下Master。你想要使用的任何东西,例如:
Master.Title = "My Title";
#2
you have to cast this.MasterPage into the type of masterpage you have, and then you can access it as you'd expect
你必须将this.MasterPage转换为你拥有的主页类型,然后你可以按照你的期望访问它
var mp = this.MasterPage as MyMasterPageType;
mp.Property = value... etc
#3
From the Page you can use the Master
property and cast this to your master page. i.e. (MyMasterPage)this.Master
. However, whenever I attempt to do this I always check it can be cast first so I normally end up with something like...
在页面中,您可以使用Master属性并将其强制转换为母版页。即(MyMasterPage)this.Master。但是,每当我尝试这样做时,我总是检查它是否可以先施放,所以我通常最终会得到类似的东西......
MyMasterPage master;
if (this.Master is MyMasterPage)
{
master = (MyMasterPage)this.Master
//do stuff with master.
}
If all you are wanting to do is change the title then you can just use Page.Title
and make sure that the head tag in your master page is set to runat='server'.
如果您想要做的就是更改标题,那么您可以使用Page.Title并确保母版页中的head标记设置为runat ='server'。
#4
In your code write :
在你的代码中写:
Dim masterpage As New MasterPage
masterpage = CType(masterpage, MasterPage)
and in your source code where the language is defined and etc type this
并在您的源代码中定义语言等,输入此内容
MasterPageFile="~/MasterPage.master"
If you write in C#
如果用C#写的话
MasterPage masterpage = new MasterPage();
masterpage = (MasterPage)masterpage;
#5
In your initial question (before it was edited) I think you mentioned "global settings". Depending on what you want to do, you may want to look into the BasePage concept as well, since I think it might be more suitable. Since you derive from it, all its members are accessible in your code-behind.
在您的初始问题(编辑之前)中,我认为您提到了“全局设置”。根据您的想法,您可能也想查看BasePage概念,因为我认为它可能更合适。由于您是从它派生的,因此可以在代码隐藏中访问其所有成员。