所以在我的默认.aspx页面上,我有几个列表框,我正在page_load上填充这些列表框。
但是,如果用户更改了这些列表框并希望还原原始设置,我希望顶部的按钮(在 Site.Master 中定义)调用相同的函数增益来还原原始值。
如何从 Site.Master 文件中获取对 _Default 对象实例的引用?有没有办法全局访问首次加载页面时调用的_Default实例?还是我需要手动将其存储在某个地方?
例如:
默认值.aspx.cs:
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
setConfigurationData();
}
public void setConfigurationData()
{
//Do stuff to elements on Default.aspx
网站.大师.cs
namespace WebApplication1
{
public partial class SiteMaster : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void RefreshMenu1_MenuItemClick(object sender, MenuEventArgs e)
{
//Need to call this function from an instance of _Default, but I don't know
//how to retrive this or save it from when it is first created
//_Default.setConfigurationData();
将此类范围的变量添加到母版页
private System.Web.UI.Page currentContentPage = new System.Web.UI.Page();
然后将此方法添加到母版页
public void childIdentity(System.Web.UI.Page childPage)
{
currentContentPage = childPage;
}
现在在默认页面的Page_Load中添加
SiteMaster masterPage = Page.Master as SiteMaster;
masterPage.childIdentity(this);
现在,母版页应该能够通过其当前 ContentPage 变量中的引用访问"默认"页上的对象。
使用名为 setConfigurationData
的虚拟方法为页面创建一个要继承的基类。然后在母版页中,将Page
对象强制转换为基类并调用方法。
public class MyBasePage : Page
{
public virtual void setConfigurationData()
{
//Default code if you want it
}
}
在您的页面中:
public partial class MyPage : MyBasePage
{
public override void setConfigurationData()
{
//You code to do whatever
}
}
母版页:
protected void RefreshMenu1_MenuItemClick(object sender, MenuEventArgs e)
{
MyBasePage basePage = (MyBasePage)Page;
basePage.setConfigurationData();
}