繁体   English   中英

母版页中的ASP.NET C#对象对基于该母版页的所有其他页面可见

[英]ASP.NET C# object in Master Page to be visible to all other pages that are based on that Master Page

我需要能够使在ASP.NET母版页中实例化的对象对应用程序中基于该母版页的所有页面都可见。 目前我在这里定义对象:

public partial class Control : System.Web.UI.MasterPage
{
    public User TheUser;

    protected void Page_Load(object sender, EventArgs e)

......但我认为这就是我出错的地方。 有人可以帮忙吗?

您最好创建一个所有页面都继承自的基页(继承自System.Web.UI.Page ),将您的TheUser对象添加到基页。

请注意,页面不会继承其母版页。

您是否尝试创建从MasterPage继承的控件? 你真正想要做的是创建一个MasterPage并为其添加一个属性:

public partial class MyMasterPage: System.Web.UI.MasterPage
{
    public string MyProperty {get;set;}
}

现在,从使用该MasterPage的页面,您只需引用MasterPage并直接访问其成员:

protected void Page_Load(object sender, EventArgs e)
{
    MyMasterPage m = Master as MyMasterPage;
    string masterProperty = m.MyProperty;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM