Dav*_*Dev 30 c# asp.net page-lifecycle
我有以下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);
}
}
Run Code Online (Sandbox Code Playgroud)
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"]);
}
}
Run Code Online (Sandbox Code Playgroud)
问题是FactsheetBase的Page_Load没有执行.
谁能告诉我我做错了什么?有没有更好的方法来获得我追求的结果?
谢谢
thi*_*eek 49
我们遇到了类似的问题,你需要做的只是在构造函数中注册处理程序.:)
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"]);
}
}
Run Code Online (Sandbox Code Playgroud)
另一种方法是重写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);
}
}
Run Code Online (Sandbox Code Playgroud)
嗯,我可能错了,但我相信这是由于继承:您正在覆盖派生类中的 FactsheetBase Page_Load 方法。
为了让它执行,你应该做类似的事情
public partial class PerformanceFactsheet : FactsheetBase
{
protected void Page_Load(object sender, EventArgs e)
{
base.Page_Load( sender, e );
// do stuff with the data extracted in FactsheetBase
divPerformance.Controls.Add(this.Data);
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:n8wrl 绝对给了你一个更简洁的解决方案(我不是 ASPX 程序员)。
| 归档时间: |
|
| 查看次数: |
32844 次 |
| 最近记录: |