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

时间:2010-04-29 12:04:55

标签: 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);
    }
}

其中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没有执行。

谁能告诉我我做错了什么?有没有更好的方法来获得我追求的结果?

由于

6 个答案:

答案 0 :(得分: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"]);             
    } 
}

另一种方法是覆盖不太喜欢的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);
    }
}

答案 1 :(得分:7)

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

答案 2 :(得分:4)

嗯,我可能错了,但我相信这是由于继承:你在覆盖派生类中的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);
    }
}
编辑:n8wrl肯定给了你一个更清洁的解决方案(我不是ASPX程序员)。

答案 3 :(得分:4)

尝试这个

 public partial class PerformanceFactsheet : FactsheetBase
{
    public PerformanceFactsheet()
    {
        this.Load += new EventHandler(this.Page_Load);
    }

    protected void Page_Load(object sender, EventArgs e)
    {            
        divPerformance.Controls.Add(this.Data);
    }
}

public abstract class FactsheetBase : System.Web.UI.Page
{
    public MyPageData Data { get; set; }
    public FactsheetBase()
    {
        this.Load += new EventHandler(this.Page_Load);
    }

    new protected void Page_Load(object sender, EventArgs e)
    {            
        this.Data = ExtractPageData(Request.QueryString["data"]);
    }
}

答案 4 :(得分:0)

试试这个:

     public partial class PerformanceFactsheet : FactsheetBase
{
    protected override 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);
    }
}

public class FactsheetBase : System.Web.UI.Page
{
    public MyPageData Data { get; set; } 
    protected virtual 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"]);            
    }
}

答案 5 :(得分:0)

将页面加载为公共,并从其他页面以这样的方式调用它:

this.myPageOrUserControl.Page_Load(null, EventArgs.Empty);