C# 手动连接 Page_PreInit 事件,并将 AutoEventWireup 设置为 false

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11841519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 19:28:10  来源:igfitidea点击:

Wiring up the Page_PreInit event manually, with AutoEventWireup set to false

c#asp.neteventsevent-handlingpage-lifecycle

提问by CptSupermrkt

If the AutoEventWireupattribute is set to false, the events need to be wired up manually. However, I cannot seem to get the Page_PreInitto fire. I would guess that I might be making the wireup happen too late (once we're already past Page_PreInit), but I'm not sure where else to put the wireups.

如果该AutoEventWireup属性设置为false,则需要手动连接事件。但是,我似乎Page_PreInit无法触发。我想我可能让接线发生得太晚了(一旦我们已经过去了Page_PreInit),但我不确定还有什么地方可以放置接线。

For example...

例如...

protected override void OnInit(EventArgs e)
{
    base.OnInit(e)
    PreInit += new EventHandler(Page_PreInit);
    Load += new EventHandler(Page_Load);
}

protected void Page_PreInit(object sender, EventArgs e)
{
    Response.Write("Page_PreInit event fired!<br>");  //this is never reached
}

protected void Page_Load(object sender, EventArgs e)
{
    Response.Write("Page_Load event fired!<br>");
}

The above code results in "Page_Load event fired!" being displayed, but nothing from Page_PreInit. I tried base.OnInit(e)both before AND after the wireups, and that had no effect.

上面的代码导致“Page_Load 事件被触发!” 正在显示,但没有来自Page_PreInit. 我base.OnInit(e)在接线之前和之后都尝试过,但没有效果。

The graph shown heresays that the OnInitmethod actually comes after the PreInitevent. With that in mind, I tried overriding OnPreInitand doing the same thing --- no effect.

此处显示的图表表示该OnInit方法实际上是在PreInit事件之后出现的。考虑到这一点,我尝试覆盖OnPreInit并做同样的事情——没有效果。

The MSDN article hereexplicitly says that in the event of AutoEventWireupset to false, the events can be wired up in an overriden OnInit. The example they use is Page_Load, and of course that works just like it does for me, but they don't address that this doesn't seem to work for the Page_PreInitevent.

此处的 MSDN 文章明确指出,在AutoEventWireup设置为false的情况下,可以将事件连接到 overriden 中OnInit。他们使用的示例是Page_Load,当然这就像对我一样,但他们没有解决这似乎对Page_PreInit事件不起作用的问题。

My question is: how can I get the Page_PreInitevent wired up in the case of AutoEventWireupset to false?

我的问题是:如何Page_PreInitAutoEventWireup设置为的情况下连接事件false

I understand there are alternatives as listed on the MSDN page, such as using the page's constructor. I'd like to know specifically how to do like they suggest with OnInit.

我知道MSDN 页面上列出了替代方法,例如使用页面的构造函数。我想具体知道如何像他们建议的那样做OnInit

采纳答案by Frédéric Hamidi

The base implementation of the OnPreInit()method is responsible for raising the PreInitevent. Since your override calls that implementation beforeregistering your PreInithandler, it will indeed not be called.

OnPreInit()方法的基本实现负责引发PreInit事件。由于您的覆盖注册PreInit处理程序之前调用了该实现,因此它确实不会被调用。

Try calling the base class' method afterregistering the handler:

注册处理程序尝试调用基类的方法:

protected override void OnPreInit(EventArgs e)
{
    PreInit += new EventHandler(Page_PreInit);
    Load += new EventHandler(Page_Load);

    // And only then:
    base.OnPreInit(e);
}

回答by Ihsan Ahmed

add all the event in initialization area:

在初始化区添加所有事件:

public WebName()
      {
         this.event+=TAB Key
      }