本节更详细地讲解了ASP.NET页面。你会学到动态编译跟引用(code-behind)文件。我们也会讨论页面类支持的事件。
理解动态编译
说来也奇怪,创建一个ASP.NET页面时,你实际上是在给一个.NET类创建源代码。你在创建一个System.Web.UI.Page类的新实例。一个asp.net页面的所有内容包括所有script及HTML内容在内,都被编译成了一个.NET类.
在请求一个asp.net页面时,asp.net Framework会检查这个页面相对的一个.net类.如果不存在对应的类,Framework会自动把页面编译为新的类并存储到ASP.NET临时文件夹里.这个文件夹位于:
/WINDOWS/Microsoft.NET/Framework/[version]/Temporary ASP.NET Files
下次有人请求相同页面时,页面就不会重新编译.编译好的类会执行并把结果返回给浏览器.
就算你拔掉你的web服务器,远远地弄到婆罗洲3年,再重新打开服务器,有人请求相同的这个页面时,页面还是不需要重新编译.编译好的类会一直保存在这个临时文件夹直到程序被修改了.
类被添加到这个文件夹后,一个文件从属关系就在类跟原始ASP.NET页面之间产生了.如果页面有了任何的修改,相应的.net类就被自动删除了.下次请求这个页面时,Framework会自动把修改后的页面源文件编译为新的.net类.
这个过程就叫做动态编译.动态编译使asp.net程序可以支持几千个不同用户的同时访问.与asp一般页面不同,在被请求时asp.net页面不需要解析跟编译.asp.net页面只有在程序被修改时才会编译.
注释
你可以使用aspnet_compiler.exe命令行工具来预编译一个完整的asp.net程序.如果程序被预编译了,用户在就不会碰到第一次请求页面时的编译延迟.
你可以通过CompilationMode属性来禁用单个页面,同个文件夹里的页面或者整个网站的动态编译.在<%@ Page %>命令里用了CompilationMode属性后,单个页面就可以禁用动态编译.在web配置文件中用了CompilationMode属性后,整个文件夹或者程序都禁用了动态编译.
在网站中有几千个页面的或你不想每个页面都载入一个assembly到内存时禁用编译就非常有用.CompilationMode被设置为Never值时,页面就不会再编译且页面不会有一个assembly产生.页面是实时解释的.
含有服务器端代码的页面无法禁用编译.特别指出的是,不编译的页面不能有服务器端的<script>...</script>代码块.此外,不编译的页面可以含有asp.net空间跟数据绑定(databinding)表达式.
如果还是有点晕的话,下面列出了FirstPage.aspx的源代码(稍微去掉了一些代码).这个文件是允许调试程序后从上面提到的临时文件夹里拷出的.
Listing 1.12. FirstPage.aspx Source
Option Strict OffOption Explicit OnImports Microsoft.VisualBasicImports SystemImports System.CollectionsImports System.Collections.SpecializedImports System.ConfigurationImports System.TextImports System.Text.RegularExpressionsImports System.WebImports System.Web.CachingImports System.Web.ProfileImports System.Web.SecurityImports System.Web.SessionStateImports System.Web.UIImports System.Web.UI.HtmlControlsImports System.Web.UI.WebControlsImports System.Web.UI.WebControls.WebPartsNamespace ASP<System.Runtime.CompilerServices.CompilerGlobalScopeAttribute()> _Public Class firstpage_aspxInherits Global.System.Web.UI.PageImplements System.Web.SessionState.IRequiresSessionState, System.Web.IHttpHandlerProtected WithEvents lblServerTime As Global.System.Web.UI.WebControls.LabelProtected WithEvents form1 As Global.System.Web.UI.HtmlControls.HtmlFormPrivate Shared __initialized As BooleanPrivate Shared __fileDependencies As ObjectSub Page_Load()lblServerTime.Text = DateTime.Now.ToString()End SubPublic Sub New()MyBase.NewDim dependencies() As StringCType(Me,System.Web.UI.Page).AppRelativeVirtualPath = "~/FirstPage.aspx"If (Global.ASP.firstpage_aspx.__initialized = false) Thendependencies = New String(0) {}dependencies(0) = "~/FirstPage.aspx"Global.ASP.firstpage_aspx.__fileDependencies =Me.GetWrappedFileDependencies(dependencies)Global.ASP.firstpage_aspx.__initialized = trueEnd IfMe.Server.ScriptTimeout = 30000000End SubProtected ReadOnly Property Profile() As System.Web.Profile.DefaultProfileGetReturn CType(Me.Context.Profile,System.Web.Profile.DefaultProfile)End GetEnd PropertyProtected ReadOnly Property ApplicationInstance() As System.Web.HttpApplicationGetReturn CType(Me.Context.ApplicationInstance,System.Web.HttpApplication)End GetEnd PropertyPrivate Function __BuildControllblServerTime() As Global.System.Web.UI.WebControls.LabelDim __ctrl As Global.System.Web.UI.WebControls.Label__ctrl = New Global.System.Web.UI.WebControls.LabelMe.lblServerTime = __ctrl__ctrl.ApplyStyleSheetSkin(Me)__ctrl.ID = "lblServerTime"Return __ctrlEnd FunctionPrivate Function __BuildControlform1() AsGlobal.System.Web.UI.HtmlControls.HtmlFormDim __ctrl As Global.System.Web.UI.HtmlControls.HtmlForm__ctrl = New Global.System.Web.UI.HtmlControls.HtmlFormMe.form1 = __ctrl__ctrl.ID = "form1"Dim __parser As System.Web.UI.IParserAccessor =CType(__ctrl,System.Web.UI.IParserAccessor)__parser.AddParsedSubObject(New System.Web.UI.LiteralControl(""&Global.Microsoft.VisualBasic.ChrW(13)&Global.Microsoft.VisualBasic.ChrW(10)&" <div>"&Global.Microsoft.VisualBasic.ChrW(13)&Global.Microsoft.VisualBasic.ChrW(10)&" "&Global.Microsoft.VisualBasic.ChrW(13)&Global.Microsoft.VisualBasic.ChrW(10)&" Welcome to ASP.NET 2.0! The current dateand time is:"&Global.Microsoft.VisualBasic.ChrW(13)&Global.Microsoft.VisualBasic.ChrW(10)&" "& _" "&Global.Microsoft.VisualBasic.ChrW(13)&Global.Microsoft.VisualBasic.ChrW(10)&" "))Dim __ctrl1 As Global.System.Web.UI.WebControls.Label__ctrl1 = Me.__BuildControllblServerTime__parser.AddParsedSubObject(__ctrl1)__parser.AddParsedSubObject(New System.Web.UI.LiteralControl(""&Global.Microsoft.VisualBasic.ChrW(13)&Global.Microsoft.VisualBasic.ChrW(10)&" "&Global.Microsoft.VisualBasic.ChrW(13)&Global.Microsoft.VisualBasic.ChrW(10)&" </div>"&Global.Microsoft.VisualBasic.ChrW(13)&Global.Microsoft.VisualBasic.ChrW(10)&" "))Return __ctrlEnd FunctionPrivate Sub __BuildControlTree(ByVal __ctrl As firstpage_aspx)Me.InitializeCultureDim __parser As System.Web.UI.IParserAccessor = CType(__ctrl,System.Web.UI.IParserAccessor)__parser.AddParsedSubObject(New System.Web.UI.LiteralControl(""&Global.Microsoft.VisualBasic.ChrW(13)&Global.Microsoft.VisualBasic.ChrW(10)&"<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN""""http://www.w3.o"& _"rg/TR/xhtml1/DTD/xhtml1-transitional.dtd"">"&Global.Microsoft.VisualBasic.ChrW(13)&Global.Microsoft.VisualBasic.ChrW(10)))__parser.AddParsedSubObject(NewSystem.Web.UI.LiteralControl(""&Global.Microsoft.VisualBasic.ChrW(13)&Global.Microsoft.VisualBasic.ChrW(10)&"<html xmlns=""http://www.w3.org/1999/xhtml"">"&Global.Microsoft.VisualBasic.ChrW(13)&Global.Microsoft.VisualBasic.ChrW(10)&"<head>"&Global.Microsoft.VisualBasic.ChrW(13)&Global.Microsoft.VisualBasic.ChrW(10)&" <title>First Page</ti"& _"tle>"&Global.Microsoft.VisualBasic.ChrW(13)&Global.Microsoft.VisualBasic.ChrW(10)&"</head>"&Global.Microsoft.VisualBasic.ChrW(13)&Global.Microsoft.VisualBasic.ChrW(10)&"<body>"&Global.Microsoft.VisualBasic.ChrW(13)&Global.Microsoft.VisualBasic.ChrW(10)&" "))Dim __ctrl1 As Global.System.Web.UI.HtmlControls.HtmlForm__ctrl1 = Me.__BuildControlform1__parser.AddParsedSubObject(__ctrl1)__parser.AddParsedSubObject(New System.Web.UI.LiteralControl(""&Global.Microsoft.VisualBasic.ChrW(13)&Global.Microsoft.VisualBasic.ChrW(10)&"</body>"&Global.Microsoft.VisualBasic.ChrW(13)&Global.Microsoft.VisualBasic.ChrW(10)&"</html>"&Global.Microsoft.VisualBasic.ChrW(13)&Global.Microsoft.VisualBasic.ChrW(10)))End SubProtected Overrides Sub FrameworkInitialize()MyBase.FrameworkInitializeMe.__BuildControlTree(Me)Me.AddWrappedFileDependencies(Global.ASP.firstpage_aspx.__fileDependencies)Me.Request.ValidateInputEnd SubPublic Overrides Function GetTypeHashCode() As IntegerReturn 579569163End FunctionPublic Overrides Sub ProcessRequest(ByVal context As System.Web.HttpContext)MyBase.ProcessRequest(context)End SubEnd ClassEnd Namespace
Listing 1.12中的类继承于System.Web.UI.Page.页面显示时ProcessRequest()方法被asp.net Framework调用.这个方法创建了页面的控件树,下一部分将以控件树为标题.