Page_PreInit不叫? [英] Page_PreInit not called?
问题描述
进出口运行一个ASP.NET 4.0项目。
在.aspx页面设置 AutoEventWireup =真
在头。
虽然 OnPreInit
被称为 Page_PreInit
是不是?任何人都可以提出什么不好?
保护无效Page_PreInit(对象发件人,EventArgs五)
{
的Response.Write(巴);
}
保护覆盖无效OnPreInit(EventArgs的发送)
{
的Response.Write(富);
base.OnPreInit(E);
}
的 Page_PreInit
事件不火,你可以看到这一点,如果你把一个断点在活动的开始,并在运行时通过它一步。
字符串bar不被写入屏幕的原因是因为它得到了 OnPreInitEvent
。
$ B覆盖$ b
请在下面看到的代码。通过它
步骤,你会发现它是如何进入 Page_PreInit
键,然后获取在 OnPreInitEvent
覆盖。
如果你注释掉 OnPreInit
事件中,你会看到Page_PreInit被写入到整屏。
使用系统;
公共部分类_Default:System.Web.UI.Page
{
串eventName的=的String.Empty;
保护无效的Page_Load(对象发件人,EventArgs五)
{
的Response.Write(eventName的);
}
保护无效Page_PreInit(对象发件人,EventArgs五)
{
eventName的=Page_PreInit;
}
保护覆盖无效OnPreInit(EventArgs的发送)
{
base.OnPreInit(E);
eventName的=OnPreInit;
}
}
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);
}
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";
}
}
这篇关于Page_PreInit不叫?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!