Why is an event not logged if it is part of a third partial file in ASP.NET?

I tried to add broken test code by creating a third partial file in addition to MyPage.aspx and MyPage.aspx.cs and MyPage.aspx.designer.cs. I named my third file MyPage.aspx.TEST.cs

In the partial file MyPage.aspx.TEST.cs, I wrote the following:

protected override void OnInit(EventArgs e) { Page.LoadComplete += RunTest; base.OnInit(e); } //Assert. public void RunTest(object sender, EventArgs e) { //Clever assertions } 

The code compiles, and then I decompile the code, and there it is, I see the OnInit override and the RunTest method.

But when I execute the page, the event is not logged, does not fire, and cannot set a breakpoint.

I transfer this code from the partial class MyPage.aspx.TEST.cs to the partial file MyPage.aspx.cs and event registers and it is executed. Stranger, when I decompile the assembly and make diff, the class seems to decompile the same code.

Possible hints that may not be related:

  • The page uses autoeventwireup = "true" (I still get the same behavior if I try to register my event in my Page_LoadComplete)
  • An application is a web application (i.e. uses a proj file)
  • The partial file is compiled (and if I introduce errors in the partial file, this will prevent compilation, so I know for sure that the partial file has been compiled)
  • I get the same result using different events (PreRender, etc.)
+6
source share
1 answer

This is strange, I just did the same experiment, and all events quit, I have the same conditions, web application, autoeventwireup = true

Are you inheriting from a different base page?

This is my incomplete class:

 public partial class _Default { protected override void OnInit(EventArgs e) { this.LoadComplete += RunTest; this.Load += new EventHandler(_Default_Load); base.OnInit(e); } void _Default_Load(object sender, EventArgs e) { //throw new NotImplementedException(); } void RunTest(object sender, EventArgs e) { //throw new NotImplementedException(); } protected override void OnPreRender(EventArgs e) { this.Response.Write("omfgggg"); this.lblMyMessageTest.Text = "omfg2"; base.OnPreRender(e); } } 

All events work if I uncomment //throw new NotImplementedException(); I get an exception as expected.

Try the following:

  • Make sure the name of your partial page classes is the same.
  • Try changing Page.LoadComplete += RunTest; on this.LoadComplete += RunTest;
  • Make sure that you do not complete the page response when an exception occurs.
  • If you have a custom HTTP module, try disabling it, it might interfere with events in some way
+2
source

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


All Articles