我的问题是我有一个动态创建内容的基本页面。我的母版页上有一些按钮可以触发我的基本页面需要知道的事件。但是我的基本页面上的 OnLoad 函数在我的母版页上的 Button_Command 函数之前触发。因此,我要么需要在 Button_Command 函数有机会设置变量后找到加载我的基本页面的方法,要么我必须从 Button_Command 函数调用我的基本页面中的一个函数。
问问题
2587 次
3 回答
0
我相信你可以用一个界面来做到这一点
public interface ICommandable
{
public void DoCommand1(object argument);
}
所以子页面实现了这个接口
public class MyPage : Page, ICommandable
{
public void DoCommand1(object argument) {
}
}
在母版页上
public class Master : MasterPage
{
public void Button_Command(object sender, EventArgs e)
{
if (Page is ICommandable)
{
(Page as ICommandable).DoCommand1(myargument);
}
else
throw new NotImplementedException("The current page does not implement ICommandable");
}
}
然而,自从我使用网络表单以来已经有很长时间了,所以我不能发誓这有效。我似乎记得以前写过这样的东西。
于 2010-04-20T15:51:19.000 回答
0
您能否简单地将 Button_Command 的逻辑封装到公共方法中,并从母版页上的 Button_Command 事件和子页上的 Load 事件调用该方法?所以像
protected void Page_Load( object sender, EventArgs e )
{
var master = (MyMaster)this.Master;
master.Foo();
}
于 2010-04-20T16:07:03.147 回答