记得最开始学习ASP.NET的时候,我们就被告知:Page_Load方法里面可以写页面加载的代码。
于是我们就懵懵懂懂写了很长时间的Page_Load方法。最近回过头思考,为什么一个普通的方法,
能被自动调用呢?于是就得知了AutoEventWireup属性。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %>
一般我们新建页面的时候,AutoEventWireup就为true。MSDN的解释是:指示控件的事件是否自动匹配 (Autowire)。
如果启用事件自动匹配,则为 true;否则为 false。默认值为 true。
那么我们先得到一个结论是:AutoEventWireup为true时,Page_Load、Page_Init之类的方法名
能被自动调用。
下面我们反编译源代码来看看里面是怎么回事。首先反编译所有页面的父类:Page类。
1
|
public
class
Page : TemplateControl, IHttpHandler { }
|
大致浏览一下,没有找到“Page_Load” 之类的字符串,说明不是在Page类处理的,继续查找Page类
的父类TemplateControl类。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
public
abstract
class
TemplateControl : Control, INamingContainer, IFilterResolutionService
{
// Fields
private
static
object
_emptyEventSingleton;
private
static
Hashtable _eventListCache;
private
static
IDictionary _eventObjects;
private
static
object
_lockObject;
private
int
_maxResourceOffset;
private
BuildResultNoCompileTemplateControl _noCompileBuildResult;
private
const
string
_onTransactionAbortEventName =
"OnTransactionAbort"
;
private
const
string
_onTransactionCommitEventName =
"OnTransactionCommit"
;
private
const
string
_pageAbortTransactionEventName =
"Page_AbortTransaction"
;
private
const
string
_pageCommitTransactionEventName =
"Page_CommitTransaction"
;
private
const
string
_pageDataBindEventName =
"Page_DataBind"
;
private
const
string
_pageErrorEventName =
"Page_Error"
;
private
const
string
_pageInitCompleteEventName =
"Page_InitComplete"
;
private
const
string
_pageInitEventName =
"Page_Init"
;
private
const
string
_pageLoadCompleteEventName =
"Page_LoadComplete"
;
private
const
string
_pageLoadEventName =
"Page_Load"
;
private
const
string
_pagePreInitEventName =
"Page_PreInit"
;
private
const
string
_pagePreLoadEventName =
"Page_PreLoad"
;
private
const
string
_pagePreRenderCompleteEventName =
"Page_PreRenderComplete"
;
private
const
string
_pagePreRenderEventName =
"Page_PreRender"
;
private
const
string
_pageSaveStateCompleteEventName =
"Page_SaveStateComplete"
;
private
const
string
_pageUnloadEventName =
"Page_Unload"
;
|
1
2
|
。。。。。。。。
}
|
找到了!里面黑茫茫一片的字符串,呵呵。继续仔细查找入口,发现了如下方法:
![](https://i-blog.csdnimg.cn/blog_migrate/cdec0645add3fc3c328197dda5c76203.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/81178cc93a2a3bb5048d90d76e7ec935.gif)