Page_PreInit not called?
Asked Answered
P

1

7

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);
}
Prudie answered 9/9, 2010 at 11:27 Comment(3)
Your syntax seems fine. Is this defined in the MasterPage?Eagan
I couldn't reproduce the issue you described. Is AutoEventWireup defined in the master page?Extremism
Or are you inheriting the page?Dispensable
G
3

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";
    }
}
Gastrocnemius answered 16/11, 2011 at 6:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.