覆盖onLoad(e)的覆盖,但调用最低的基础实现

时间:2016-05-06 18:54:17

标签: c# asp.net method-overriding

我在覆盖方法时遇到问题。我创建了一个基类,其中包含我想在OnLoad事件期间运行的自定义代码。此重写方法中的代码适用于从其继承的90%的页面,但在几个页面上我需要覆盖覆盖。我的问题是我仍然需要运行System.Web.UI.Page的OnLoad实现。如果我在第二个类中包含base.OnLoad(e)行(见下文),它会调用BasePageEdit的逻辑,但是如果我删除它,则永远不会调用Page_Load事件。我如何跳过覆盖BaseEditPage中的逻辑,仍然从System.Web.UI.Page获取功能?

public class BasePageEdit : System.Web.UI.Page
{
    protected override void OnLoad(EventArgs e)
    {
     // I need this to raise the Page_Load Event!
     base.OnLoad(e); // Calls System.Web.UI.Page OnLoad Event which I want.

    // Logic that I want to run in ADDITION to base Implementation;

    }

    // Other classes and methods;
}

public class WebPageThatNeedsSpecialOnLoadImplementation : BasePageEdit
{
     protected override void OnLoad(EventArgs e)
     {
     // I need this to raise the Page_Load Event!
     base.OnLoad(e); // If I include this, it runs the BasePageEdit, I don't want that...
     // But I still need to run the System.Web.UI.Page onLoad event or Page_Load will not be called.

     // Logic that I want to run INSTEAD of the logic from the override from BasePageEdit.
     }
}

非常感谢Slaks!我正在编辑我的问题,以显示我如何实施,以便观看此帖子的其他人如果选择可以实现相同的方式!

public class BasePageEdit : System.Web.UI.Page
{
    protected override void OnLoad(EventArgs e)
    {
    BasePage_OnLoad(e);

    // Logic that I want to run in ADDITION to base Implementation;

    }

    protected virtual void BasePage_OnLoad(EventArgs e)
    {
         base.OnLoad(e);
    }

    // Other classes and methods;
}

public class WebPageThatNeedsSpecialOnLoadImplementation : BasePageEdit
{
     protected override void OnLoad(EventArgs e)
     {
         BasePage_OnLoad(e);

     // Logic that I want to run INSTEAD of the logic from the override from BasePageEdit.
     }
}

1 个答案:

答案 0 :(得分:4)

你做不到。

但是,您可以在base.OnLoad中创建一个新方法,只需调用 map(),然后直接从派生类中调用它。