Posting the Page_PreInit event manually, with the AutoEventWireup parameter - false

If the AutoEventWireup attribute AutoEventWireup set to false , events must be AutoEventWireup manually. However, it seems I can not start Page_PreInit . I would suggest that I could do the wiring too late (as soon as we have already passed Page_PreInit ), but I'm not sure where else to put the wired devices.

For instance...

 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 causes the "Page_Load event fired!" But nothing from Page_PreInit . I tried base.OnInit(e) both before and after connecting, and without effect.

The graph shown here says that the OnInit method actually occurs after the PreInit event. With that in mind, I tried to override OnPreInit and do the same --- no effect.

The MSDN article here explicitly states that with AutoEventWireup set to false , events can be hooked into overriden OnInit . The example they use is Page_Load , and of course it works the same as it does for me, but they do not address that it does not work for the Page_PreInit event.

My question is: how can I get the Page_PreInit event in case of AutoEventWireup set to false ?

I understand that there are alternatives listed on the MSDN page, for example, using the page constructor. I would like to know specifically how to do how they offer using OnInit .

+6
source share
2 answers

The main implementation of the method

+10
source

add the whole event to the initialization area:

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

Source: https://habr.com/ru/post/922261/


All Articles