重写 onLoad(e) 的重写,但调用最低的基本实现
Override an override of onLoad(e), but call the lowest base implementation
我在重写方法时遇到问题。我创建了一个基础 class,其中包含我希望在 OnLoad 事件期间 运行 的自定义代码。这个重写方法中的代码适用于 90% 从它继承的页面,但在少数页面上我需要重写重写。我的问题是我仍然需要 运行 System.Web.UI.Page 的 OnLoad 实现。如果我在第二个 class 中包含 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!我正在编辑我的问题以展示我是如何实施的,以便其他查看此 post 的人可以选择以相同的方式实施!
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.
}
}
你不能那样做。
但是,您可以在 BasePageEdit
中创建一个新方法,它只调用 its base.OnLoad
,然后直接从派生的 class 中调用它] 相反。
我在重写方法时遇到问题。我创建了一个基础 class,其中包含我希望在 OnLoad 事件期间 运行 的自定义代码。这个重写方法中的代码适用于 90% 从它继承的页面,但在少数页面上我需要重写重写。我的问题是我仍然需要 运行 System.Web.UI.Page 的 OnLoad 实现。如果我在第二个 class 中包含 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!我正在编辑我的问题以展示我是如何实施的,以便其他查看此 post 的人可以选择以相同的方式实施!
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.
}
}
你不能那样做。
但是,您可以在 BasePageEdit
中创建一个新方法,它只调用 its base.OnLoad
,然后直接从派生的 class 中调用它] 相反。