之前在asp.net 1.1的页面上会有一个页面事件注册的方法
protected override void OnInit(EventArgs e)
{
this.Load += new EventHandler(Page_Load);
base.OnInit(e);
}
asp.net 2.0 中 **.aspx.cs 和 **.aspx.design.cs里面都没有。考虑应该已经在框架内部实现了。
所以找到源代码看了一下。在TemplateControler.cs里面有如下代码
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
static string [] methodNames =
{ "Page_Init",
#if NET_2_0
"Page_PreInit",
"Page_PreLoad",
"Page_LoadComplete",
"Page_PreRenderComplete",
"Page_SaveStateComplete",
"Page_InitComplete",
#endif
"Page_Load",
"Page_DataBind",
"Page_PreRender",
"Page_Disposed",
"Page_Error",
"Page_Unload",
"Page_AbortTransaction",
"Page_CommitTransaction"};
看名字很熟悉把。原来已经约定俗成在这里了。
继续查找 methodNames
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
private ArrayList CollectAutomaticEventInfo ()
{
ArrayList events = new ArrayList ();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
foreach (string methodName in methodNames)
{
MethodInfo method = null;
Type type;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for (type = GetType (); type.Assembly != _System_Web_Assembly; type = type.BaseType)
{
method = type.GetMethod (methodName, bflags);
if (method != null)
break;
}
if (method == null)
continue;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (method.DeclaringType != type)
{
if (!method.IsPublic && !method.IsFamilyOrAssembly &&
!method.IsFamilyAndAssembly && !method.IsFamily)
continue;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (method.ReturnType != typeof (void))
continue;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ParameterInfo [] parms = method.GetParameters ();
int length = parms.Length;
bool noParams = (length == 0);
if (!noParams && (length != 2 ||
parms [0].ParameterType != typeof (object) ||
parms [1].ParameterType != typeof (EventArgs)))
continue;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int pos = methodName.IndexOf ("_");
string eventName = methodName.Substring (pos + 1);
EventInfo evt = type.GetEvent (eventName);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (evt == null)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//* This should never happen */
continue;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
EvtInfo evinfo = new EvtInfo ();
evinfo.method = method;
evinfo.methodName = methodName;
evinfo.evt = evt;
evinfo.noParams = noParams;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
events.Add (evinfo);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return events;
}
反射获取方法后。经过一系列的检查。然后注册到事件上。
放在内部实现挺好。页面上的代码更简洁了。不过如果不了解页面执行流程的话估计很难理解了。
转自:http://www.cnblogs.com/sky266/archive/2008/09/23/1296635.html