开发者

Page_PreInit not called?

开发者 https://www.devze.com 2023-01-15 17:08 出处:网络
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 n开发者_如何转开发ot? Can anyone suggest what is wron

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 n开发者_如何转开发ot? 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);
}


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";
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号