动态母版页,可以访问两个母版中的变量
Dynamic Master Page with access to variables in both masters
我有两个母版页,一个用于桌面,另一个用于移动,
并且取决于我使用的设备动态 桌面版或移动版母版页。
但是我无法从母版页中的子 ASPX 页面访问变量。
背后的 ASPX 代码:
protected override void OnPreInit(EventArgs e)
{
if (Request.Browser.IsMobileDevice)
{
this.MasterPageFile = "MobileMasterPage.master";
}
else
{
this.MasterPageFile = "MasterPageDesktop.master";
}
}
并且在两个母版页中都有变量
public int TinyMceWidth { get; set; }
public int TinyMceHeight { get; set; }
但我无法从 ASPX 的代码中访问这两个变量:
protected void Page_Load(object sender, EventArgs e)
{
Master.TinyMceWidth = 1000; // Can't access
Master.TinyMceHeight = 1000; // Can't access
}
我该如何解决?
您可以这样访问它。
var master = (Site1)Page.Master;
master.TinyMceWidth = 1000;
Site1
是母版页的 class 名称
public partial class Site1 : System.Web.UI.MasterPage
{
}
所以因为你有 2 个主控,你需要为它们创建 2 个变量
var masterMobile = (Site2)Page.Master;
我有两个母版页,一个用于桌面,另一个用于移动, 并且取决于我使用的设备动态 桌面版或移动版母版页。 但是我无法从母版页中的子 ASPX 页面访问变量。
背后的 ASPX 代码:
protected override void OnPreInit(EventArgs e)
{
if (Request.Browser.IsMobileDevice)
{
this.MasterPageFile = "MobileMasterPage.master";
}
else
{
this.MasterPageFile = "MasterPageDesktop.master";
}
}
并且在两个母版页中都有变量
public int TinyMceWidth { get; set; }
public int TinyMceHeight { get; set; }
但我无法从 ASPX 的代码中访问这两个变量:
protected void Page_Load(object sender, EventArgs e)
{
Master.TinyMceWidth = 1000; // Can't access
Master.TinyMceHeight = 1000; // Can't access
}
我该如何解决?
您可以这样访问它。
var master = (Site1)Page.Master;
master.TinyMceWidth = 1000;
Site1
是母版页的 class 名称
public partial class Site1 : System.Web.UI.MasterPage
{
}
所以因为你有 2 个主控,你需要为它们创建 2 个变量
var masterMobile = (Site2)Page.Master;