如何在页面基类中执行Page_Load()?

发布于 2024-08-31 05:37:37 字数 833 浏览 3 评论 0原文

我有以下 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 未执行。

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

谢谢

I have the following PerformanceFactsheet.aspx.cs page class

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);
    }
}

where FactsheetBase is defined as

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"]);            
    }
}

The problem is that FactsheetBase's Page_Load is not executing.

Can anyone tell me what I'm doing wrong? Is there a better way to get the result I'm after?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

狂之美人 2024-09-07 05:37:38

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

Uhm, I maybe wrong, but I believe this is due to inheritance: you are overwriting the FactsheetBase Page_Load method in the derived class.

In order to have it executed you should do something like

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);
    }
}

EDIT: n8wrl definitely gave you a cleaner solution (I am not a ASPX programmer).

兮颜 2024-09-07 05:37:38

试试这个

 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"]);
    }
}

try this one

 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"]);
    }
}
如痴如狂 2024-09-07 05:37:38

试试这个:

     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"]);            
    }
}

try this one:

     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"]);            
    }
}
看透却不说透 2024-09-07 05:37:38

使页面加载公开,并从其他页面以如下方式调用它:

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

Make the page load public, and call it in a manner like this from the other page:

this.myPageOrUserControl.Page_Load(null, EventArgs.Empty);
绾颜 2024-09-07 05:37:37

我们遇到了类似的问题,您所需要做的就是在构造函数中注册处理程序。 :)

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);
    }
}

We faced the similar problem, All you need to do is just register the handler in the constructor. :)

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"]);             
    } 
}

Another approach would be to override OnLoad() which is less preferred.

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);
    }
}
深海里的那抹蓝 2024-09-07 05:37:37

替代 Page_Load() 方法,重写 OnLoad() 并调用 PerformanceFactsheet 中的 base.OnLoad()

Instead of a Page_Load() method, override OnLoad() and call base.OnLoad() in PerformanceFactsheet

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文