重写 Page_PreInit 时出现编译器错误
我试图重写继承自 Page
的类 _Default
中的 Page_PreInit
函数。但是,当我尝试编译时,出现以下错误:
“_Default.Page_PreInit(object, System.EventArgs)”:找不到合适的方法来覆盖
这是我的代码:
public partial class _Default : Page
{
protected override void Page_PreInit(object sender, EventArgs e)
{
// Todo:
// The _Default class overrides the Page_PreInit method and sets the value
// of the MasterPageFile property to the current value in the
// selectedLayout session variable.
MasterPageFile = Master.Session["selectedLayout"];
}
...
}
I am trying to override the Page_PreInit
function inside my class _Default
which inherits from Page
. However, when I try to compile I get the following error:
'_Default.Page_PreInit(object, System.EventArgs)': no suitable method found to override
Here is my code:
public partial class _Default : Page
{
protected override void Page_PreInit(object sender, EventArgs e)
{
// Todo:
// The _Default class overrides the Page_PreInit method and sets the value
// of the MasterPageFile property to the current value in the
// selectedLayout session variable.
MasterPageFile = Master.Session["selectedLayout"];
}
...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Page
类声明一个名为PreInit
和名为OnPreInit
(仅引发PreInit
事件)。所以你有两个选择。选项 1(推荐): 覆盖
OnPreInit
:调用
base.OnPreInit(e)
以便页面引发PreInit
> 活动如常。选项 2:创建一个名为
Page_PreInit
的方法。只要您未在@ 中将
指令或在 Web.config 中。AutoEventWireup
设置为False
,ASP.NET 就会自动将此方法绑定到PreInit
事件。 Page如果您选择此选项,请不要调用
base.OnPreInit
,否则您将得到无限递归。The
Page
class declares a public event namedPreInit
and a protected virtual method namedOnPreInit
(which just raises thePreInit
event). So you have two options.Option 1 (recommended): Override
OnPreInit
:Call
base.OnPreInit(e)
so that the page raises thePreInit
event as usual.Option 2: Create a method named
Page_PreInit
. ASP.NET will automatically bind this method to thePreInit
event as long as you don't setAutoEventWireup
toFalse
in the@Page
directive or in Web.config.If you choose this option, don't call
base.OnPreInit
, or else you will end up with an infinite recursion.