开发者

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 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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜