我有以下PerformanceFactsheet.aspx.cs页面类
public partial class PerformanceFactsheet : FactsheetBase
{
protected void Page_Load(object sender, EventArgs e)
{
// do stuff with the data extracted in FactsheetBase
divPerformance.Controls.Add(this.Data);
}
}
其中FactsheetBase被定义为
public class FactsheetBase : System.Web.UI.Page
{
public MyPageData Data { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
// get data that's common to all implementors of FactsheetBase
// and store the values in FactsheetBase's properties
this.Data = ExtractPageData(Request.QueryString["data"]);
}
}
问题是FactsheetBase的Page_Load没有执行。
谁能告诉我我在做什么错?有没有更好的方法来获得我想要的结果?
谢谢
最佳答案
我们遇到了类似的问题,所有您需要做的就是在构造函数中注册处理程序。 :)
public class FactsheetBase : System.Web.UI.Page
{
public FactsheetBase()
{
this.Load += new EventHandler(this.Page_Load);
}
public MyPageData Data { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
// get data that's common to all implementors of FactsheetBase
// and store the values in FactsheetBase's properties
this.Data = ExtractPageData(Request.QueryString["data"]);
}
}
另一种方法是覆盖不太受欢迎的OnLoad()。
public class FactsheetBase : System.Web.UI.Page
{
public FactsheetBase()
{
}
public MyPageData Data { get; set; }
protected override void OnLoad(EventArgs e)
{
//your code
// get data that's common to all implementors of FactsheetBase
// and store the values in FactsheetBase's properties
this.Data = ExtractPageData(Request.QueryString["data"]);
base.OnLoad(e);
}
}
关于c# - 如何在Page的基类中执行Page_Load()?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2737092/