Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why might an event not register when it is a part of a 3rd partial file in ASP.NET?

Tags:

c#

asp.net

events

I was trying to add some non-production test code by creating a 3rd partial file in addition to MyPage.aspx and MyPage.aspx.cs and MyPage.aspx.designer.cs. I called 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 I then decompile the code and there it is, I can see the OnInit override and the RunTest method.

But when I execute the page, the event doesn't register, nor run, nor can I set a breakpoint.

I move that code out of the MyPage.aspx.TEST.cs partial class into the MyPage.aspx.cs partial file and the event registers and is executed. Stranger, when I decompile the assembly, and do a diff, the class appears to decompile to the same code.

Possible clues that may be unrelated:

  • The page uses autoeventwireup="true" (I still get the same behavior if I try to register my event my Page_LoadComplete)
  • The application is a web application (i.e.uses a proj file)
  • The partial file does compile (and if I introduce errors into the partial file, it will prevent compilation, so I know for sure that the partial file does get compiled)
  • I get the same result using different events (PreRender, etc)
like image 695
MatthewMartin Avatar asked Jun 19 '12 22:06

MatthewMartin


1 Answers

This is strange, I just made the same experiment and all the events are being fired, I have the same conditions, web application, autoeventwireup = true

Are you inheriting from another base page?

This is my partial 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 the events works, if I uncomment the //throw new NotImplementedException(); I get the exception as expected.

Try the following:

  • Ensure the name of your partial page classes are the same
  • Try to change Page.LoadComplete += RunTest; to this.LoadComplete += RunTest;
  • Ensure you are not terminating the response of the page when an exception occurs
  • If you have a custom HTTP module, try to disable it, it might be interfering with the events somehow
like image 67
Jupaol Avatar answered Sep 21 '22 10:09

Jupaol