C# 是否可以在从asp.net WebFroms中的特定页面子类继承的页面的页面加载事件上调用函数?

C# 是否可以在从asp.net WebFroms中的特定页面子类继承的页面的页面加载事件上调用函数?,c#,asp.net,webforms,C#,Asp.net,Webforms,我想在每个System.Web.UI.Page的Page\u load事件上执行一个函数,从中派生出我自己的CustomPage类(显然也从Page类继承) 到目前为止,我所做的就是创建如下CustomPage类: public class CustomPage : System.Web.UI.Page { protected virtual void Page_Load(object sender, EventArgs e) { CallTOTheDesired

我想在每个
System.Web.UI.Page
Page\u load
事件上执行一个函数,从中派生出我自己的
CustomPage
类(显然也从
Page
类继承)

到目前为止,我所做的就是创建如下
CustomPage
类:

public class CustomPage : System.Web.UI.Page
{
    protected virtual void Page_Load(object sender, EventArgs e)
    {
        CallTOTheDesiredFunction();  //this is the call to the function I want
    }
} 
在派生的
页面
类中,我正在这样做:

public class DerivedPage : CustomPage
{
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        //the rest of the page load event which executes from here on
    }
} 
很明显,这种方法是有效的,但它不是最好的解决方案,因为我必须在每个派生页面上调用
base.Page\u Load(sender,e)

对于我想要实现的目标,有没有更好的解决方案?
提前谢谢你

是的。最好重写
Onload
方法,而不是依赖派生类来调用基方法

您仍然可以在每个页面中钩住Load事件,但在基类中使用该方法

public class CustomPage : System.Web.UI.Page
{
    protected override void OnLoad(EventArgs e)
    {
        CallTOTheDesiredFunction();  //this is the call to the function I want

        base.OnLoad(e);
    }
}