webform的PageController模式

原始出处Behind the scenes of ASPX files

Asp.net Page文件通常有两个文件,aspx文件定义外观,cs文件((Code behind文件)处理事件,运行时,每一个cs文件会被编译成dll文件。

当page第一次被访问的时候,
1:.net会根据aspx文件生成一个cs文件
2:用csc.exe把这个cs文件编译成dll
3: 运行编译生成的这个dll

上面的过程只有第一次请求页面时才发生,所以第一次访问某个page时会感觉比较慢。
以后.net就用dll来处理对这个页面的请求,如果aspx文件有变化,,net会重新生成dll文件

生成的这个dll文件可以在
C:\$WINDOWSDir$\Microsoft.NET\Framework\v1.1.4322\Temporary ASP.NET Files\$YourWebAppName$\4449be44\81bf2529
(最后面两级目录的名字是随机的)
目录下找到,我们可以看到这个目录下除了cs文件外还有其他类型的文件,其中

*.cs:??根据aspx文件生成的cs文件
*.res ??资源文件
*cmdline ?用来编译*.cs文件的命令行
*.err? ??编译*.cs的错误
*out ??编译时的输出文件
*.dll ??编译生成的文件
*pdb ??编译生成的文件
*.xml??存储aspx文件名和.net生成的用来命名文件的随机数之间的映射关系

我们可以看到Temporary ASP.NET Files\$YourWebAppName$\目录下的文件,除了xml文件之外都是以随机数字作为文件名,所以需要一个page

的实际名字与随机数字之间的映射关系,这个映射存储在xml文件中。
比如


???
???

我们根据这个映射,找到生成的cs文件

从cs文件里,我们可以看到,cs文件中标出了与aspx原文件相对应的行号
比如

aspx文件中第三行为

相应的cs文件中
#line 3 "F:\CRM\TestControl.ascx"
__ctrl = new System.Web.UI.WebControls.TextBox();
.............

另一方面,我们可以注意到每一个根据aspx文件生成的类都继承了aspx文件的code behind类,并且实现了其接口。

比如:???
public class TestControl_ascx : CRM.TestControl
public class WebForm1_aspx : CRM.WebForm1, System.Web.SessionState.IRequiresSessionState

根据aspx文件生成的类先于code behide类被调用,其构造函数会初始化所依赖的文件

public WebForm1_aspx() {
?System.Collections.ArrayList dependencies;
??????????? if ((ASP.WebForm1_aspx.__initialized == false)) {
??????????????? ASP.WebForm1_aspx.__stringResource = System.Web.UI.TemplateControl.ReadStringResource(typeof(ASP.WebForm1

_aspx));
??????????????? dependencies = new System.Collections.ArrayList();
??????????????? dependencies.Add("F:\\CRM\\webform1.aspx");
??????????????? dependencies.Add("F:\\CRM\\bin\\CRM.DLL");
??????????????? dependencies.Add("F:\\CRM\\TestControl.ascx");
??????????????? ASP.WebForm1_aspx.__fileDependencies = dependencies;
??????????????? ASP.WebForm1_aspx.__initialized = true;
??????????? }
??????????? this.Server.ScriptTimeout = 30000000;
??????? }


其构造函数执行后,会执行override的函数FrameworkInitialize,
函数FrameworkInitialize调用__BuildControlTree 来构造所有页面上的控件,

综上所述,请求aspx页面时,有两个类参与
1:code behind class ?– WebForm1.
2:从ASPX生成的类?– adbdef.

执行的顺序为
1:adbdef的构造函数
2:WebForm1的构造函数
3:adbdef类的方法FrameworkInitialize
4:FrameworkInitialize调用__ BuildTree创建各个控件
5:按顺序调用Page和controls的事件处理程序,ASPX中声明的事件先被处理

附录:
testcontrol.ascx

<%@ Control Language="c#" AutoEventWireup="false" Codebehind="TestControl.ascx.cs" Inherits="CRM.TestControl" TargetSchema="

http://schemas.microsoft.com/intellisense/ie5"%>
Label




testcontrol.ascx.cs

protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.TextBox TextBox1;

private void Page_Load(object sender, System.EventArgs e)
{
?// Put user code to initialize the page here
}

//Web Form Designer generated code

private void Button1_Click(object sender, System.EventArgs e)
{
?Label1.Text = TextBox1.Text.ToLower();?
}

public void InitializeControl()
{?
?TextBox1.Text = "Test it";
}?


webform1.aspx
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="CRM.WebForm1" %>
<%@ Register TagPrefix="uc1" TagName="TestControl" Src="TestControl.ascx" %>


?
??
?
?
??
???
??
?

webform1.aspx.cs
public class WebForm1 : System.Web.UI.Page
{
?protected CRM.TestControl TestControl1;
?
?private void Page_Load(object sender, System.EventArgs e)
?{
??if (! Page.IsPostBack)
??{
???TestControl1.InitializeControl();
??}
?}

?//Web Form Designer generated code
?
}


TestControl.ascx.7c4db376.xml xml映射文件


???
???


根据aspx文件生成的zogvrdep.0.cs

//------------------------------------------------------------------------------
//
//???? This code was generated by a tool.
//???? Runtime Version: 1.1.4322.573
//
//???? Changes to this file may cause incorrect behavior and will be lost if
//???? the code is regenerated.
//

//------------------------------------------------------------------------------

namespace ASP {
??? using System;
??? using System.Collections;
??? using System.Collections.Specialized;
??? using System.Configuration;
??? using System.Text;
??? using System.Text.RegularExpressions;
??? using System.Web;
??? using System.Web.Caching;
??? using System.Web.SessionState;
??? using System.Web.Security;
??? using System.Web.UI;
??? using System.Web.UI.WebControls;
??? using System.Web.UI.HtmlControls;
??? using ASP;
???
???
??? [System.Runtime.CompilerServices.CompilerGlobalScopeAttribute()]
??? public class TestControl_ascx : CRM.TestControl {
???????
??????? private static bool __initialized = false;
???????
??????? public TestControl_ascx() {
??????????? if ((ASP.TestControl_ascx.__initialized == false)) {
??????????????? ASP.TestControl_ascx.__initialized = true;
??????????? }
??????? }
???????
??????? protected override bool SupportAutoEvents {
??????????? get {
??????????????? return false;
??????????? }
??????? }
???????
??????? protected ASP.Global_asax ApplicationInstance {
??????????? get {
??????????????? return ((ASP.Global_asax)(this.Context.ApplicationInstance));
??????????? }
??????? }
???????
??????? public override string TemplateSourceDirectory {
??????????? get {
??????????????? return "/crm";
??????????? }
??????? }
???????
??????? private System.Web.UI.Control __BuildControlLabel1() {
??????????? System.Web.UI.WebControls.Label __ctrl;
???????????
??????????? #line 2 "F:\CRM\TestControl.ascx"
??????????? __ctrl = new System.Web.UI.WebControls.Label();
???????????
??????????? #line default
??????????? #line hidden
??????????? this.Label1 = __ctrl;
???????????
??????????? #line 2 "F:\CRM\TestControl.ascx"
??????????? __ctrl.ID = "Label1";
???????????
??????????? #line default
??????????? #line hidden
??????????? System.Web.UI.IParserAccessor __parser = ((System.Web.UI.IParserAccessor)(__ctrl));
???????????
??????????? #line 2 "F:\CRM\TestControl.ascx"
??????????? __parser.AddParsedSubObject(new System.Web.UI.LiteralControl("Label"));
???????????
??????????? #line default
??????????? #line hidden
??????????? return __ctrl;
??????? }
???????
??????? private System.Web.UI.Control __BuildControlTextBox1() {
??????????? System.Web.UI.WebControls.TextBox __ctrl;
???????????
??????????? #line 3 "F:\CRM\TestControl.ascx"
??????????? __ctrl = new System.Web.UI.WebControls.TextBox();
???????????
??????????? #line default
??????????? #line hidden
??????????? this.TextBox1 = __ctrl;
???????????
??????????? #line 3 "F:\CRM\TestControl.ascx"
??????????? __ctrl.ID = "TextBox1";
???????????
??????????? #line default
??????????? #line hidden
??????????? return __ctrl;
??????? }
???????
??????? private System.Web.UI.Control __BuildControlButton1() {
??????????? System.Web.UI.WebControls.Button __ctrl;
???????????
??????????? #line 4 "F:\CRM\TestControl.ascx"
??????????? __ctrl = new System.Web.UI.WebControls.Button();
???????????
??????????? #line default
??????????? #line hidden
??????????? this.Button1 = __ctrl;
???????????
??????????? #line 4 "F:\CRM\TestControl.ascx"
??????????? __ctrl.ID = "Button1";
???????????
??????????? #line default
??????????? #line hidden
???????????
??????????? #line 4 "F:\CRM\TestControl.ascx"
??????????? __ctrl.Text = "Button";
???????????
??????????? #line default
??????????? #line hidden
??????????? return __ctrl;
??????? }
???????
??????? private void __BuildControlTree(System.Web.UI.Control __ctrl) {
???????????
??????????? #line 1 "F:\CRM\TestControl.ascx"
??????????? this.__BuildControlLabel1();
???????????
??????????? #line default
??????????? #line hidden
??????????? System.Web.UI.IParserAccessor __parser = ((System.Web.UI.IParserAccessor)(__ctrl));
???????????
??????????? #line 1 "F:\CRM\TestControl.ascx"
??????????? __parser.AddParsedSubObject(this.Label1);
???????????
??????????? #line default
??????????? #line hidden
???????????
??????????? #line 1 "F:\CRM\TestControl.ascx"
??????????? __parser.AddParsedSubObject(new System.Web.UI.LiteralControl("
\r\n"));
???????????
??????????? #line default
??????????? #line hidden
???????????
??????????? #line 1 "F:\CRM\TestControl.ascx"
??????????? this.__BuildControlTextBox1();
???????????
??????????? #line default
??????????? #line hidden
???????????
??????????? #line 1 "F:\CRM\TestControl.ascx"
??????????? __parser.AddParsedSubObject(this.TextBox1);
???????????
??????????? #line default
??????????? #line hidden
???????????
??????????? #line 1 "F:\CRM\TestControl.ascx"
??????????? __parser.AddParsedSubObject(new System.Web.UI.LiteralControl("
\r\n"));
???????????
??????????? #line default
??????????? #line hidden
???????????
??????????? #line 1 "F:\CRM\TestControl.ascx"
??????????? this.__BuildControlButton1();
???????????
??????????? #line default
??????????? #line hidden
???????????
??????????? #line 1 "F:\CRM\TestControl.ascx"
??????????? __parser.AddParsedSubObject(this.Button1);
???????????
??????????? #line default
??????????? #line hidden
???????????
??????????? #line 1 "F:\CRM\TestControl.ascx"
??????????? __parser.AddParsedSubObject(new System.Web.UI.LiteralControl("\r\n"));
???????????
??????????? #line default
??????????? #line hidden
??????? }
???????
??????? protected override void FrameworkInitialize() {
??????????? this.__BuildControlTree(this);
??????? }
??? }
}

zogvrdep.cmdline 文件

/t:library /utf8output /R:"c:\windows\microsoft.net\framework\v1.1.4322\mscorlib.dll" /R:"c:\windows\assembly\gac\system.

web\1.0.5000.0__b03f5f7f11d50a3a\system.web.dll" /R:"c:\windows\assembly\gac\system\1.0.5000.0__b77a5c561934e089\system.dll"

/R:"c:\windows\assembly\gac\system.web.services\1.0.5000.0__b03f5f7f11d50a3a\system.web.services.dll" /R:"c:

\windows\assembly\gac\microsoft.web.ui.webcontrols\1.0.2.226__31bf3856ad364e35\microsoft.web.ui.webcontrols.dll" /R:"c:

\windows\assembly\gac\system.xml\1.0.5000.0__b77a5c561934e089\system.xml.dll" /R:"c:\windows\microsoft.net\framework\v1.1.

4322\temporary asp.net files\crm\4449be44\81bf2529\assembly\dl2\8bac978f\041be9f8_1687c301\webcontrols.dll" /R:"c:

\windows\assembly\gac\system.drawing\1.0.5000.0__b03f5f7f11d50a3a\system.drawing.dll" /R:"c:\windows\assembly\gac\system.web

.mobile\1.0.5000.0__b03f5f7f11d50a3a\system.web.mobile.dll" /R:"c:\windows\assembly\gac\system.data\1.0.5000.0__b77a5c561934

e089\system.data.dll" /R:"c:\windows\microsoft.net\framework\v1.1.4322\temporary asp.net files\crm\4449be44\81bf2529

\assembly\dl2\5f1e7c76\408f8112_6e82c301\crm.dll" /R:"c:\windows\microsoft.net\framework\v1.1.4322\temporary asp.net

files\crm\4449be44\81bf2529\assembly\dl2\a65bb180\78075961_8585c301\metacontrols.dll" /R:"c:\windows\assembly\gac\system.

enterpriseservices\1.0.5000.0__b03f5f7f11d50a3a\system.enterpriseservices.dll" /R:"c:\windows\microsoft.net\framework\v1.1.

4322\temporary asp.net files\crm\4449be44\81bf2529\rpxfbzqs.dll" /out:"C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\Temporary

ASP.NET Files\crm\4449be44\81bf2529\zogvrdep.dll" /D咧嘴笑脸EBUG /debug+ /optimize- /warnaserror /w:1? "C:\WINDOWS\Microsoft.

NET\Framework\v1.1.4322\Temporary ASP.NET Files\crm\4449be44\81bf2529\zogvrdep.0.cs"

zogvrdep.out 文件
C:\WINDOWS\system32> "c:\windows\microsoft.net\framework\v1.1.4322\csc.exe" /t:library /utf8output /R:"c:\windows\microsoft.

net\framework\v1.1.4322\mscorlib.dll" /R:"c:\windows\assembly\gac\system.web\1.0.5000.0__b03f5f7f11d50a3a\system.web.dll" /R

:"c:\windows\assembly\gac\system\1.0.5000.0__b77a5c561934e089\system.dll" /R:"c:\windows\assembly\gac\system.web.services\1.

0.5000.0__b03f5f7f11d50a3a\system.web.services.dll" /R:"c:\windows\assembly\gac\microsoft.web.ui.webcontrols\1.0.2.226__31bf

3856ad364e35\microsoft.web.ui.webcontrols.dll" /R:"c:\windows\assembly\gac\system.xml\1.0.5000.0__b77a5c561934e089\system.

xml.dll" /R:"c:\windows\microsoft.net\framework\v1.1.4322\temporary asp.net files\crm\4449be44\81bf2529\assembly\dl2\8bac978

f\041be9f8_1687c301\webcontrols.dll" /R:"c:\windows\assembly\gac\system.drawing\1.0.5000.0__b03f5f7f11d50a3a\system.drawing.

dll" /R:"c:\windows\assembly\gac\system.web.mobile\1.0.5000.0__b03f5f7f11d50a3a\system.web.mobile.dll" /R:"c:

\windows\assembly\gac\system.data\1.0.5000.0__b77a5c561934e089\system.data.dll" /R:"c:\windows\microsoft.net\framework\v1.1.

4322\temporary asp.net files\crm\4449be44\81bf2529\assembly\dl2\5f1e7c76\408f8112_6e82c301\crm.dll" /R:"c:\windows\microsoft

.net\framework\v1.1.4322\temporary asp.net files\crm\4449be44\81bf2529\assembly\dl2\a65bb180\78075961_8585c301\metacontrols.

dll" /R:"c:\windows\assembly\gac\system.enterpriseservices\1.0.5000.0__b03f5f7f11d50a3a\system.enterpriseservices.dll" /R:"c

:\windows\microsoft.net\framework\v1.1.4322\temporary asp.net files\crm\4449be44\81bf2529\rpxfbzqs.dll" /out:"C:

\WINDOWS\Microsoft.NET\Framework\v1.1.4322\Temporary ASP.NET Files\crm\4449be44\81bf2529\zogvrdep.dll" /D咧嘴笑脸EBUG /debug+ /

optimize- /warnaserror /w:1? "C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\Temporary ASP.NET Files\crm\4449be44\81bf2529

\zogvrdep.0.cs"


Microsoft (R) Visual C# .NET Compiler version 7.10.3052.4
for Microsoft (R) .NET Framework version 1.1.4322
Copyright 咖啡 Microsoft Corporation 2001-2002. All rights reserved.


根据webform1.aspx生成的类
//------------------------------------------------------------------------------
//
//???? This code was generated by a tool.
//???? Runtime Version: 1.1.4322.573
//
//???? Changes to this file may cause incorrect behavior and will be lost if
//???? the code is regenerated.
//

//------------------------------------------------------------------------------

namespace ASP {
??? using System;
??? using System.Collections;
??? using System.Collections.Specialized;
??? using System.Configuration;
??? using System.Text;
??? using System.Text.RegularExpressions;
??? using System.Web;
??? using System.Web.Caching;
??? using System.Web.SessionState;
??? using System.Web.Security;
??? using System.Web.UI;
??? using System.Web.UI.WebControls;
??? using System.Web.UI.HtmlControls;
??? using ASP;
???
???
??? [System.Runtime.CompilerServices.CompilerGlobalScopeAttribute()]
??? public class WebForm1_aspx : CRM.WebForm1, System.Web.SessionState.IRequiresSessionState {
???????
???????
??????? #line 13 "F:\CRM\webform1.aspx"
??????? protected System.Web.UI.HtmlControls.HtmlForm Form1;
???????
??????? #line default
??????? #line hidden
???????
??????? private static bool __initialized = false;
???????
??????? private static object __stringResource;
???????
??????? private static System.Collections.ArrayList __fileDependencies;
???????
??????? public WebForm1_aspx() {
??????????? System.Collections.ArrayList dependencies;
??????????? if ((ASP.WebForm1_aspx.__initialized == false)) {
??????????????? ASP.WebForm1_aspx.__stringResource = System.Web.UI.TemplateControl.ReadStringResource(typeof(ASP.WebForm1

_aspx));
??????????????? dependencies = new System.Collections.ArrayList();
??????????????? dependencies.Add("F:\\CRM\\webform1.aspx");
??????????????? dependencies.Add("F:\\CRM\\bin\\CRM.DLL");
??????????????? dependencies.Add("F:\\CRM\\TestControl.ascx");
??????????????? ASP.WebForm1_aspx.__fileDependencies = dependencies;
??????????????? ASP.WebForm1_aspx.__initialized = true;
??????????? }
??????????? this.Server.ScriptTimeout = 30000000;
??????? }
???????
??????? protected override bool SupportAutoEvents {
??????????? get {
??????????????? return false;
??????????? }
??????? }
???????
??????? protected ASP.Global_asax ApplicationInstance {
??????????? get {
??????????????? return ((ASP.Global_asax)(this.Context.ApplicationInstance));
??????????? }
??????? }
???????
??????? public override string TemplateSourceDirectory {
??????????? get {
??????????????? return "/crm";
??????????? }
??????? }
???????
??????? private System.Web.UI.Control __BuildControlTestControl1() {
??????????? ASP.TestControl_ascx __ctrl;
???????????
??????????? #line 15 "F:\CRM\webform1.aspx"
??????????? __ctrl = new ASP.TestControl_ascx();
???????????
??????????? #line default
??????????? #line hidden
??????????? this.TestControl1 = __ctrl;
???????????
??????????? #line 15 "F:\CRM\webform1.aspx"
??????????? this.TestControl1.InitializeAsUserControl(this.Page);
???????????
??????????? #line default
??????????? #line hidden
???????????
??????????? #line 15 "F:\CRM\webform1.aspx"
??????????? __ctrl.ID = "TestControl1";
???????????
??????????? #line default
??????????? #line hidden
??????????? return __ctrl;
??????? }
???????
??????? private System.Web.UI.Control __BuildControlForm1() {
??????????? System.Web.UI.HtmlControls.HtmlForm __ctrl;
???????????
??????????? #line 13 "F:\CRM\webform1.aspx"
??????????? __ctrl = new System.Web.UI.HtmlControls.HtmlForm();
???????????
??????????? #line default
??????????? #line hidden
??????????? this.Form1 = __ctrl;
???????????
??????????? #line 13 "F:\CRM\webform1.aspx"
??????????? __ctrl.ID = "Form1";
???????????
??????????? #line default
??????????? #line hidden
???????????
??????????? #line 13 "F:\CRM\webform1.aspx"
??????????? __ctrl.Method = "post";
???????????
??????????? #line default
??????????? #line hidden
??????????? System.Web.UI.IParserAccessor __parser = ((System.Web.UI.IParserAccessor)(__ctrl));
???????????
??????????? #line 13 "F:\\CRM\webform1.aspx"
??????????? __parser.AddParsedSubObject(new System.Web.UI.LiteralControl("\r\n\t\t\t

FONT> \r\n\t\t\t"));
???????????
??????????? #line default
??????????? #line hidden
???????????
??????????? #line 13 "F:\\CRM\webform1.aspx"
??????????? this.__BuildControlTestControl1();
???????????
??????????? #line default
??????????? #line hidden
???????????
??????????? #line 13 "F:\\CRM\webform1.aspx"
??????????? __parser.AddParsedSubObject(this.TestControl1);
???????????
??????????? #line default
??????????? #line hidden
???????????
??????????? #line 13 "F:\\CRM\webform1.aspx"
??????????? __parser.AddParsedSubObject(new System.Web.UI.LiteralControl("\r\n\t\t"));
???????????
??????????? #line default
??????????? #line hidden
??????????? return __ctrl;
??????? }
???????
??????? private void __BuildControlTree(System.Web.UI.Control __ctrl) {
??????????? System.Web.UI.IParserAccessor __parser = ((System.Web.UI.IParserAccessor)(__ctrl));
???????????
??????????? #line 1 "F:\CRM\webform1.aspx"
??????????? __parser.AddParsedSubObject(this.CreateResourceBasedLiteralControl(0, 396, true));
???????????
??????????? #line default
??????????? #line hidden
???????????
??????????? #line 1 "F:\CRM\webform1.aspx"
??????????? this.__BuildControlForm1();
???????????
??????????? #line default
??????????? #line hidden
???????????
??????????? #line 1 "F:\CRM\webform1.aspx"
??????????? __parser.AddParsedSubObject(this.Form1);
???????????
??????????? #line default
??????????? #line hidden
???????????
??????????? #line 1 "F:\CRM\webform1.aspx"
??????????? __parser.AddParsedSubObject(new System.Web.UI.LiteralControl("\r\n\t\r\n\r\n"));
???????????
??????????? #line default
??????????? #line hidden
??????? }
???????
??????? protected override void FrameworkInitialize() {
??????????? SetStringResourcePointer(ASP.WebForm1_aspx.__stringResource, 396);
??????????? this.__BuildControlTree(this);
??????????? this.FileDependencies = ASP.WebForm1_aspx.__fileDependencies;
??????????? this.EnableViewStateMac = true;
??????????? this.Request.ValidateInput();
??????? }
???????
??????? public override int GetTypeHashCode() {
??????????? return -2138995558;
??????? }
??? }
}

posted on 2007-12-22 10:03  EricGu  阅读(440)  评论(0编辑  收藏  举报

导航