Page_PreInit not called?

北城余情 提交于 2020-01-02 02:35:09

问题


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);
}

回答1:


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


来源:https://stackoverflow.com/questions/3676060/page-preinit-not-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!