So I'm wiring up my first MasterPage, and everything is working great except for one thing. This is a legacy app, and I have an old BasePage class that all my content pages inherit. It inherits from System.Web.UI.Page, but has no content itself (no .aspx file). It runs a bunch of the user authentication/role granting menu building. I want to keep this functionality but use it to set controls on my MasterPage to build out the menus. I cannot for the life of me figure out how to reference the MasterPage properties without a MasterType declaration in a content page.
所以我正在连接我的第一个MasterPage,除了一件事,一切都很好。这是一个遗留应用程序,我有一个旧的BasePage类,我的所有内容页面都继承。它继承自System.Web.UI.Page,但本身没有内容(没有.aspx文件)。它运行一堆用户身份验证/角色授予菜单构建。我想保留此功能,但使用它来设置我的MasterPage上的控件以构建菜单。我不能为我的生活弄清楚如何在内容页面中引用没有MasterType声明的MasterPage属性。
My MasterPage class is called NIMS_Master, and I have the following in it (just trying to get started):
我的MasterPage类叫做NIMS_Master,我在其中有以下内容(只是尝试开始):
public partial class NIMS_Master : System.Web.UI.MasterPage { public string MenuList { get; set; }
public partial class NIMS_Master:System.Web.UI.MasterPage {public string MenuList {get;组; }
protected void Page_Load(object sender, EventArgs e)
{}
}
With a MasterType declaration in one of my content pages:
<%@ MasterType VirtualPath="~/NIMS_Master.master" %>
在我的一个内容页面中使用MasterType声明:<%@ MasterType VirtualPath =“〜/ NIMS_Master.master”%>
I can access my property in Login.aspx.cs as follows:
我可以在Login.aspx.cs中访问我的属性,如下所示:
this.Master.MenuList = "this is the menu list";
this.Master.MenuList =“这是菜单列表”;
But in my BasePage.cs, I have nowhere to put the MasterType declaration. All the google searches indicate I have to cast my NIMS_Master class as a Master, but I cannot get it to work to save my life. I've tried several different things, but my NIMS_Master class just doesn't show up in BasePage.
但在我的BasePage.cs中,我无处可放置MasterType声明。所有的谷歌搜索都表明我必须将我的NIMS_Master课程作为硕士课程,但我无法让它来拯救我的生命。我尝试了几种不同的东西,但是我的NIMS_Master类没有出现在BasePage中。
((this.Master)NIMS_Master).MenuList = "This is a menu list";
((this.Master)NIMS_Master).MenuList =“这是一个菜单列表”;
BasePage.cs is in my App_Code directory and my MasterPage file is in the application root, if that matters.
如果重要的话,BasePage.cs在我的App_Code目录中,而我的MasterPage文件在应用程序根目录中。
3 个解决方案
#1
As I see it, your cast should be: ((NIMS_Master)this.Master).MenuList
在我看来,你的演员应该是:((NIMS_Master)this.Master).MenuList
#2
So I had my BasePage in App_Code and was trying to set a property of my NIMS_Master class which was in the application root directory. I had to create a MasterBase class that inherits from System.Web.UI.MasterPage in App_Code and put my properties there. Sheesh, I'm not even sure why I thought I could access those class properties from within app_code.
所以我在App_Code中有了我的BasePage,并且正在尝试设置我的NIMS_Master类的属性,该属性位于应用程序根目录中。我必须创建一个继承自App_Code中的System.Web.UI.MasterPage的MasterBase类,并将我的属性放在那里。 Sheesh,我甚至不确定为什么我认为我可以从app_code中访问这些类属性。
#3
The MasterBase worked for me too, thanks fr0man.
MasterBase也为我工作,谢谢fr0man。
In my case, I had a public property in my master.aspx.cs file which I moved to a new the MasterBase class so that it could be exposed to other classes.
在我的情况下,我在master.aspx.cs文件中有一个公共属性,我将其移动到一个新的MasterBase类,以便它可以暴露给其他类。
Step 1: Create a new MasterBase.cs file in your App_Code dir. (The following is my complete class.)
步骤1:在App_Code目录中创建一个新的MasterBase.cs文件。 (以下是我的完整课程。)
public partial class MasterBase : System.Web.UI.MasterPage
{
public string MyProperty = "Default";
}
Step 2: Update the Master.aspx.cs file to inherit from your base class instead of System.Web.UI.MasterPage, and remove any property declarations that you have moved to the MasterBase class. (The following is the first few lines of the class.)
步骤2:更新Master.aspx.cs文件以继承基类而不是System.Web.UI.MasterPage,并删除已移至MasterBase类的所有属性声明。 (以下是该课程的前几行。)
public partial class Master : MasterBase
{
//The following is now defined in MasterBase.
//public string MyProperty = "Default";
...
Step 3: Cast the page.Master to MasterBase to access it's properties.
第3步:将page.Master转换为MasterBase以访问它的属性。
...
IHttpHandler page = HttpContext.Current.Handler;
if (page is System.Web.UI.Page)
{
MasterBase masterPage = (MasterBase)((System.Web.UI.Page)page).Master;
if (masterPage != null)
masterPage.MyProperty = "New Value";
}
...
#1
As I see it, your cast should be: ((NIMS_Master)this.Master).MenuList
在我看来,你的演员应该是:((NIMS_Master)this.Master).MenuList
#2
So I had my BasePage in App_Code and was trying to set a property of my NIMS_Master class which was in the application root directory. I had to create a MasterBase class that inherits from System.Web.UI.MasterPage in App_Code and put my properties there. Sheesh, I'm not even sure why I thought I could access those class properties from within app_code.
所以我在App_Code中有了我的BasePage,并且正在尝试设置我的NIMS_Master类的属性,该属性位于应用程序根目录中。我必须创建一个继承自App_Code中的System.Web.UI.MasterPage的MasterBase类,并将我的属性放在那里。 Sheesh,我甚至不确定为什么我认为我可以从app_code中访问这些类属性。
#3
The MasterBase worked for me too, thanks fr0man.
MasterBase也为我工作,谢谢fr0man。
In my case, I had a public property in my master.aspx.cs file which I moved to a new the MasterBase class so that it could be exposed to other classes.
在我的情况下,我在master.aspx.cs文件中有一个公共属性,我将其移动到一个新的MasterBase类,以便它可以暴露给其他类。
Step 1: Create a new MasterBase.cs file in your App_Code dir. (The following is my complete class.)
步骤1:在App_Code目录中创建一个新的MasterBase.cs文件。 (以下是我的完整课程。)
public partial class MasterBase : System.Web.UI.MasterPage
{
public string MyProperty = "Default";
}
Step 2: Update the Master.aspx.cs file to inherit from your base class instead of System.Web.UI.MasterPage, and remove any property declarations that you have moved to the MasterBase class. (The following is the first few lines of the class.)
步骤2:更新Master.aspx.cs文件以继承基类而不是System.Web.UI.MasterPage,并删除已移至MasterBase类的所有属性声明。 (以下是该课程的前几行。)
public partial class Master : MasterBase
{
//The following is now defined in MasterBase.
//public string MyProperty = "Default";
...
Step 3: Cast the page.Master to MasterBase to access it's properties.
第3步:将page.Master转换为MasterBase以访问它的属性。
...
IHttpHandler page = HttpContext.Current.Handler;
if (page is System.Web.UI.Page)
{
MasterBase masterPage = (MasterBase)((System.Web.UI.Page)page).Master;
if (masterPage != null)
masterPage.MyProperty = "New Value";
}
...