如何在Page的基类中执行Page_Load()?

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)

  • 我同意*忘记调用base.OnLoad(e)*不太受欢迎,但值得注意的是,重写`OnLoad`可让您控制代码执行的时间 - 您可以说是否希望您的代码在之前或之后运行其他`Load`处理程序.这可能并不常见,但它与**不同**,并不比添加另一个处理程序更糟或更好. (11认同)
  • 因为在那种方法中你可能忘记调用base.OnLoad(e)会对页面对象产生负面影响.并且Load事件在Page对象上公开,您可以在其上注册所需的处理程序,这样您就不会对已存在的任何内容进行任何操作,而是在事件上添加自己的处理程序. (4认同)
  • 为什么说压倒"OnLoad"不太受欢迎? (3认同)
  • 使用第一个代码块,如果您注册一个名为 Page_Load 的 EventHandler,那么您将收到一条警告,指出“'FactsheetBase .Page_Load(object, System.EventArgs)' 隐藏继承的成员 'PerformanceFactsheet.Page_Load(object, System.EventArgs)'。如果打算隐藏,请使用 new 关键字。”。为避免出现警告,您可以将基类中的 Load 处理程序的名称更改为其他名称,例如 BasePage_Load,它仍然可以正常工作 (2认同)

n8w*_*wrl 7

而不是Page_Load()方法,重写OnLoad()并在PerformanceFactsheet中调用base.OnLoad()


NeX*_*XuS 5

嗯,我可能错了,但我相信这是由于继承:您正在覆盖派生类中的 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 程序员)。