Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Page_PreInit not called?

Im running an ASP.NET 4.0 project.

The .aspx page has AutoEventWireup="true" set in the header.

Although OnPreInit is called, Page_PreInit is not? Can anyone suggest what is wrong?

protected void Page_PreInit(object sender, EventArgs e)
{
    Response.Write("bar");
}

protected override void OnPreInit(EventArgs e)
{
    Response.Write("foo");
    base.OnPreInit(e);
}
like image 654
maxp Avatar asked Sep 09 '10 11:09

maxp


1 Answers

The Page_PreInit event does fire, you can see this, if you put a breakpoint at the start of the event and step through it at the run time. The reason the string "bar" is not being written to the screen is because it gets overwritten by the OnPreInitEvent.

Please see code below. Step through it and you will notice how it goes into Page_PreInit and then gets overwritten in the OnPreInitEvent. If you comment out the ENTIRE OnPreInit event you will see "Page_PreInit" being written to the screen.

using System;

public partial class _Default : System.Web.UI.Page 
{
    string eventName = String.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write(eventName);
    }

    protected void Page_PreInit(object sender, EventArgs e)
    {
        eventName = "Page_PreInit";
    }  

    protected override void OnPreInit(EventArgs e)
    {
       base.OnPreInit(e);
       eventName = "OnPreInit";
    }
}
like image 142
Denys Wessels Avatar answered Sep 23 '22 09:09

Denys Wessels