
·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> Asp.Net原理Version3.0_页面声明周期
asp.net原理Version1.0
Asp.Net原理Version2.0
相关源码
页面的PRocess方法
1 // System.Web.UI.Page
2 private void ProcessRequest(bool includeStagesBeforeAsyncPoint, bool includeStagesAfterAsyncPoint)
3 {
4 if (includeStagesBeforeAsyncPoint)
5 {
6 //调用子类的方法(这是个虚方法),1,创建页面控件树
7 this.FrameworkInitialize();
8 base.ControlState = ControlState.FrameworkInitialized;
9 }
10 bool flag = this.Context.WorkerRequest is IIS7WorkerRequest;
11 try
12 {
13 try
14 {
15 if (this.IsTransacted)
16 {
17 this.ProcessRequestTransacted();
18 }
19 else
20 {
21 //页面声明周期的主要部分
22 this.ProcessRequestMain(includeStagesBeforeAsyncPoint, includeStagesAfterAsyncPoint);
23 }
24 if (includeStagesAfterAsyncPoint)
25 {
26 flag = false;
27 this.ProcessRequestEndTrace();
28 }
29 }
30 catch (ThreadAbortException)
31 {
32 try
33 {
34 if (flag)
35 {
36 this.ProcessRequestEndTrace();
37 }
38 }
39 catch
40 {
41 }
42 }
43 finally
44 {
45 if (includeStagesAfterAsyncPoint)
46 {
47 this.ProcessRequestCleanup();
48 }
49 }
50 }
51 catch
52 {
53 throw;
54 }
55 }
View Code
ProcessRequestMain方法
1 // System.Web.UI.Page
2 private void ProcessRequestMain(bool includeStagesBeforeAsyncPoint, bool includeStagesAfterAsyncPoint)
3 {
4 try
5 {
6 HttpContext context = this.Context;
7 string text = null;
8 if (includeStagesBeforeAsyncPoint)
9 {
10 if (this.IsInAspCompatMode)
11 {
12 AspCompatapplicationStep.OnPageStartsessionObjects();
13 }
14 if (this.PageAdapter != null)
15 {
16 this._requestValueCollection = this.PageAdapter.DeterminePostBackMode();
17 }
18 else
19 {
20 this._requestValueCollection = this.DeterminePostBackMode();
21 }
22 string text2 = string.Empty;
23 if (this.DetermineIsExportingWebPart())
24 {
25 if (!RuntimeConfig.GetAppConfig().WebParts.EnableExport)
26 {
27 throw new InvalidOperationException(SR.GetString("WebPartExportHandler_DisabledExportHandler"));
28 }
29 text = this.Request.QueryString["webPart"];
30 if (string.IsNullOrEmpty(text))
31 {
32 throw new InvalidOperationException(SR.GetString("WebPartExportHandler_InvalidArgument"));
33 }
34 if (string.Equals(this.Request.QueryString["scope"], "shared", StringComparison.OrdinalIgnoreCase))
35 {
36 this._pageFlags.Set(4);
37 }
38 string text3 = this.Request.QueryString["query"];
39 if (text3 == null)
40 {
41 text3 = string.Empty;
42 }
43 this.Request.QueryStringText = text3;
44 context.Trace.IsEnabled = false;
45 }
46
47 //2,确定IsCallback值
48 if (this._requestValueCollection != null)
49 {
50 if (this._requestValueCollection["__VIEWSTATEENCRYPTED"] != null)
51 {
52 this.ContainsEncryptedViewState = true;
53 }
54 text2 = this._requestValueCollection["__CALLBACKID"];
55 if (text2 != null && this._request.HttpVerb == HttpVerb.POST)
56 {
57 this._isCallback = true;
58 }
59 else
60 {
61 if (!this.IsCrossPagePostBack)
62 {
63 VirtualPath virtualPath = null;
64 if (this._requestValueCollection["__PREVIOUSPAGE"] != null)
65 {
66 try
67 {
68 virtualPath = VirtualPath.CreateNonRelativeAllowNull(Page.DecryptString(this._requestValueCollection["__PREVIOUSPAGE"]));
69 }
70 catch
71 {
72 this._pageFlags[8] = true;
73 }
74 if (virtualPath != null && virtualPath != this.Request.CurrentExecutionFilePathObject)
75 {
76 this._pageFlags[8] = true;
77 this._previousPagePath = virtualPath;
78 }
79 }
80 }
81 }
82 }
83 if (this.MaintainScrollPositionOnPostBack)
84 {
85 this.LoadScrollPosition();
86 }
87 if (context.TraceIsEnabled)
88 {
89 this.Trace.Write("aspx.page", "Begin PreInit");
90 }
91 if (EtwTrace.IsTraceEnabled(5, 4))
92 {
93 EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_PRE_INIT_ENTER, this._context.WorkerRequest);
94 }
95
96 //3.1初始化PreInit
97 this.PerformPreInit();
98 if (EtwTrace.IsTraceEnabled(5, 4))
99 {
100 EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_PRE_INIT_LEAVE, this._context.WorkerRequest);
101 }
102 if (context.TraceIsEnabled)
103 {
104 this.Trace.Write("aspx.page", "End PreInit");
105 }
106 if (context.TraceIsEnabled)
107 {
108 this.Trace.Write("aspx.page", "Begin Init");
109 }
110 if (EtwTrace.IsTraceEnabled(5, 4))
111 {
112 EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_INIT_ENTER, this._context.WorkerRequest);
113 }
114 //3.2递归初始化,因为要初始化树,所以用递归
115 this.InitRecursive(null);
116 if (EtwTrace.IsTraceEnabled(5, 4))
117 {
118 EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_INIT_LEAVE, this._context.WorkerRequest);
119 }
120 if (context.TraceIsEnabled)
121 {
122 this.Trace.Write("aspx.page", "End Init");
123 }
124 if (context.TraceIsEnabled)
125 {
126 this.Trace.Write("aspx.page", "Begin InitComplete");
127 }
128 //3.3,初始化完成
129 this.OnInitComplete(EventArgs.Empty);
130 if (context.TraceIsEnabled)
131 {
132 this.Trace.Write("aspx.page", "End InitComplete");
133 }
134 if (this.IsPostBack)
135 {
136 if (context.TraceIsEnabled)
137 {
138 this.Trace.Write("aspx.page", "Begin LoadState");
139 }
140 if (EtwTrace.IsTraceEnabled(5, 4))
141 {
142 EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_LOAD_VIEWSTATE_ENTER, this._context.WorkerRequest);
143 }
144 //4.1,加载ViewState
145 this.LoadAllState();
146 if (EtwTrace.IsTraceEnabled(5, 4))
147 {
148 EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_LOAD_VIEWSTATE_LEAVE, this._context.WorkerRequest);
149 }
150 if (context.TraceIsEnabled)
151 {
152 this.Trace.Write("aspx.page", "End LoadState");
153 this.Trace.Write("aspx.page", "Begin ProcessPostData");
154 }
155 if (EtwTrace.IsTraceEnabled(5, 4))
156 {
157 EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_LOAD_POSTDATA_ENTER, this._context.WorkerRequest);
158 }
159 //4.2,处理回发数据
160 this.ProcessPostData(this._requestValueCollection, true);
161 if (EtwTrace.IsTraceEnabled(5, 4))
162 {
163 EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_LOAD_POSTDATA_LEAVE, this._context.WorkerRequest);
164 }
165 if (context.TraceIsEnabled)
166 {
167 this.Trace.Write("aspx.page", "End ProcessPostData");
168 }
169 }
170 if (context.TraceIsEnabled)
171 {
172 this.Trace.Write("aspx.page", "Begin PreLoad");
173 }
174 //5.1页面预加载
175 this.OnPreLoad(EventArgs.Empty);
176 if (context.TraceIsEnabled)
177 {
178 this.Trace.Write("aspx.page", "End PreLoad");
179 }
180 if (context.TraceIsEnabled)
181 {
182 this.Trace.Write("aspx.page", "Begin Load");
183 }
184 if (EtwTrace.IsTraceEnabled(5, 4))
185 {
186 EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_LOAD_ENTER, this._context.WorkerRequest);
187 }
188 //5.2页面递归加载
189 this.LoadRecursive();
190 if (EtwTrace.IsTraceEnabled(5, 4))
191 {
192 EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_LOAD_LEAVE, this._context.WorkerRequest);
193 }
194 if (context.TraceIsEnabled)
195 {
196 this.Trace.Write("aspx.page", "End Load");
197 }
198 if (this.IsPostBack)
199 {
200 if (context.TraceIsEnabled)
201 {
202 this.Trace.Write("aspx.page", "Begin ProcessPostData Second Try");
203 }
204 //5.3第二次尝试加载回发数据
205 this.ProcessPostData(this._leftoverPostData, false);
206 if (context.TraceIsEnabled)
207 {
208 this.Trace.Write("aspx.page", "End ProcessPostData Second Try");
209 this.Trace.Write("aspx.page", "Begin Raise ChangedEvents");
210 }
211 if (EtwTrace.IsTraceEnabled(5, 4))
212 {
213 EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_POST_DATA_CHANGED_ENTER, this._context.WorkerRequest);
214 }
215 //6.1,触发Change事件
216 this.RaiseChangedEvents();
217 if (EtwTrace.IsTraceEnabled(5, 4))
218 {
219 EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_POST_DATA_CHANGED_LEAVE, this._context.WorkerRequest);
220 }
221 if (context.TraceIsEnabled)
222 {
223 this.Trace.Write("aspx.page", "End Raise ChangedEvents");
224 this.Trace.Write("aspx.page", "Begin Raise PostBackEvent");
225 }
226 if (EtwTrace.IsTraceEnabled(5, 4))
227 {
228 EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_RAISE_POSTBACK_ENTER, this._context.WorkerRequest);
229 }
230 //6.2,触发点击事件
231 this.RaisePostBackEvent(this._requestValueCollection);
232 if (EtwTrace.IsTraceEnabled(5, 4))
233 {
234 EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_RAISE_POSTBACK_LEAVE, this._context.WorkerRequest);
235 }
236 if (context.TraceIsEnabled)
237 {
238 this.Trace.Write("aspx.page", "End Raise PostBackEvent");
239 }
240 }
241 if (context.TraceIsEnabled)
242 {
243 this.Trace.Write("aspx.page", "Begin LoadComplete");
244 }
245 //5.3,页面加载完成
246 this.OnLoadComplete(EventArgs.Empty);
247 if (context.TraceIsEnabled)
248 {
249 this.Trace.Write("aspx.page", "End LoadComplete");
250 }
251 if (this.IsPostBack && this.IsCallback)
252 {
253 this.PrepareCallback(text2);
254 }
255 else
256 {
257 if (!this.IsCrossPagePostBack)
258 {
259 if (context.TraceIsEnabled)
260 {
261 this.Trace.Write("aspx.page", "Begin PreRender");
262 }
263 if (EtwTrace.IsTraceEnabled(5, 4))
264 {
265 EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_PRE_RENDER_ENTER, this._context.WorkerRequest);
266 }
267
268 //7,递归预渲染
269 this.PreRenderRecursiveInternal();
270 if (EtwTrace.IsTraceEnabled(5, 4))
271 {
272 EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_PRE_RENDER_LEAVE, this._context.WorkerRequest);
273 }
274 if (context.TraceIsEnabled)
275 {
276 this.Trace.Write("aspx.page", "End PreRender");
277 }
278 }
279 }
280 }
281 if (this._asyncInfo == null || this._asyncInfo.CallerIsBlocking)
282 {
283 this.ExecuteRegisteredAsyncTasks();
284 }
285 this._request.ValidateRawUrl();
286 if (includeStagesAfterAsyncPoint)
287 {
288 if (this.IsCallback)
289 {
290 this.RenderCallback();
291 }
292 else
293 {
294 if (!this.IsCrossPagePostBack)
295 {
296 if (context.TraceIsEnabled)
297 {
298 this.Trace.Write("aspx.page", "Begin PreRenderComplete");
299 }
300 this.PerformPreRenderComplete();
301 if (context.TraceIsEnabled)
302 {
303 this.Trace.Write("aspx.page", "End PreRenderComplete");
304 }
305 if (context.TraceIsEnabled)
306 {
307 this.BuildPageProfileTree(this.EnableViewState);
308 this.Trace.Write("aspx.page", "Begin SaveState");
309 }
310 if (EtwTrace.IsTraceEnabled(5, 4))
311 {
312 EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_SAVE_VIEWSTATE_ENTER, this._context.WorkerRequest);
313 }
314 //8,保存页面状态到ViewState
315 this.SaveAllState();
316 if (EtwTrace.IsTraceEnabled(5, 4))
317 {
318 EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_SAVE_VIEWSTATE_LEAVE, this._context.WorkerRequest);
319 }
320 if (context.TraceIsEnabled)
321 {
322 this.Trace.Write("aspx.page", "End SaveState");
323 this.Trace.Write("aspx.page", "Begin SaveStateComplete");
324 }
325 this.OnSaveStateComplete(EventArgs.Empty);
326 if (context.TraceIsEnabled)
327 {
328 this.Trace.Write("aspx.page", "End SaveStateComplete");
329 this.Trace.Write("aspx.page", "Begin Render");
330 }
331 if (EtwTrace.IsTraceEnabled(5, 4))
332 {
333 EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_RENDER_ENTER, this._context.WorkerRequest);
334 }
335 if (text != null)
336 {
337 this.ExportWebPart(text);
338 }
339 else
340 {
341 //9,渲染出HTML
342 this.RenderControl(this.CreateHtmlTextWriter(this.Response.Output));
343 }
344 if (EtwTrace.IsTraceEnabled(5, 4))
345 {
346 EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_RENDER_LEAVE, this._context.WorkerRequest);
347 }
348 if (context.TraceIsEnabled)
349 {
350 this.Trace.Write("aspx.page", "End Render");
351 }
352 this.CheckRemainingAsyncTasks(false);
353 }
354 }
355 }
356 }
357 catch (ThreadAbortException ex2)
358 {
359 HttpApplication.CancelModuleException ex = ex2.ExceptionState as HttpApplication.CancelModuleException;
360 if (!includeStagesBeforeAsyncPoint || !includeStagesAfterAsyncPoint || this._context.Handler != this || this._context.ApplicationInstance == null || ex == null || ex.Timeout)
361 {
362 this.CheckRemainingAsyncTasks(true);
363 throw;
364 }
365 this._context.ApplicationInstance.CompleteRequest();
366 Page.ThreadResetAbortWithAssert();
367 }
368 catch (ConfigurationException)
369 {
370 throw;
371 }
372 catch (Exception e)
373 {
374 PerfCounters.IncrementCounter(AppPerfCounter.ERRORS_DURING_REQUEST);
375 PerfCounters.IncrementCounter(AppPerfCounter.ERRORS_TOTAL);
376 if (!this.HandleError(e))
377 {
378 throw;
379 }
380 }
381 }
View Code