页面生命周期 ---Scott Mitchell ---翻译:yocan

当一个请求到达ASP.NET Web服务器时,请求会被交给ASP.NET引擎处理,经历包括文件访问检查、用户Session恢复等多个阶段的处理管线。最后,引擎实例化对应的页面类并执行ProcessRequest()方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

Each time a request arrives at a Web server for an ASP.NET Web page, the first thing the Web server does is hand off the request to the ASP.NET engine. The ASP.NET engine then takes the request through a pipeline composed of numerous stages, which includes verifying file access rights for the ASP.NET Web page, resurrecting the user's session state, and so on. At the end of the pipeline, a class corresponding to the requested ASP.NET Web page is instantiated and the ProcessRequest() method is invoked (see Figure 1).

每当一个请求asp.net页面的请求到达web服务器时,web服务器首先做的是将这个请求转送到asp.net engine。Asp.net engine接到该请求后,会将该请求进行一系列的处理,其中包括验证被请求的asp.net页面的访问权;重建该用户的session状态等等。在这一系列处理的最后,实例化了与被请求的asp.net页面相对应的类被并且调用PrcessRequest()方法。

  
Figure 1. ASP.NET Page Handling
一:处理 asp.net 页面
This life cycle of the ASP.NET page starts with a call to the ProcessRequest() method. This method begins by initializing the page's control hierarchy. Next, the page and its server controls proceed lock-step through various phases that are essential to executing an ASP.NET Web page. These steps include managing view state, handling postback events, and rendering the page's HTML markup. Figure 2 provides a graphical representation of the ASP.NET page life cycle. The life cycle ends by handing off the Web page's HTML markup to the Web server, which sends it back to the client that requested the page.
Asp.net页面的生命周期起始于ProcessRequst()方法的调用。这个方法以初始化页面控件层次架构(page’s control hierarchy)开始(--译者注:下面将介绍什么是control hierarchy)。接下来,页面和它上面的服务器控件会经历一系列阶段,这些阶段对于执行asp.net web页面来说是十分必要的。这些阶段包括:管理viewstate,处理回发事件,和页面html代码的呈现。下面图片展示了asp.net 页面生命周期。这个生命周期结束于将web页面的html代码发到web服务器(服务器再将html代码发到请求该页面的客户端)
Note    A detailed discussion of the steps leading up to the ASP.NET page life cycle is beyond the scope of this article. For more information read Michele Leroux-Bustamante's Inside IIS & ASP.NET. For a more detailed look at HTTP handlers, which are the endpoints of the ASP.NET pipeline, check out my previous article on HTTP Handlers.
What is important to realize is that each and every time an ASP.NET Web page is requested it goes through these same life cycle stages (shown in Figure 2).
 
Figure 2. Events in the Page Life Cycle
二.生命周期中的事件
Stage 0 – Instantiation (译者笔记:初次访问时,实例化页面。 Asp.net Engine 把由 html 标签和 web 控件组成的整个页面转换为一个 class ,这个类将会在下次访问此页面时创建该页面的 control hierarchy ,即创建页面的控件)
The life cycle of the ASP.NET page begins with instantiation of the class that represents the requested ASP.NET Web page, but how is this class created? Where is it stored?
Asp.net 页面的生命周期开始于实例化一个类,这个类描述了被请求的 asp.netweb 页面,那么这个类是怎样创建的?它有存储在哪里?
ASP.NET Web pages, as you know, are made up of both an HTML portion and a code portion, with the HTML portion containing HTML markup and Web control syntax. The ASP.NET engine converts the HTML portion from its free-form text representation into a series of programmatically-created Web controls.
正如你所知, asp.net web 页面是由 html 部分和代码部分组成的,其中 html 部分包含了 html 标签和 web 控件的语法。 Asp.net 引擎将 html 部分从自由形态的文本描述,转换到一系列程序创建的 web 控件。(此段翻译有点不通,请读者参看原文理解,不正确之处还望指出)
When an ASP.NET Web page is visited for the first time after a change has been made to the HTML markup or Web control syntax in the .aspx page, the ASP.NET engine auto-generates a class . If you created your ASP.NET Web page using the code-behind technique, this autogenerated class is derived from the page's associated code-behind class (note that the code-behind class must be derived itself, either directly or indirectly, from the System.Web.UI.Page class); if you created your page with an in-line, server-side <script> block, the class derives directly from System.Web.UI.Page . In either case, this autogenerated class, along with a compiled instance of the class, is stored in the WINDOWS /Microsoft.NET/Framework/version/Temporary ASP.NET Files folder, in part so that it doesn't need to be recreated for each page request.
 
当更改 .aspx 页的 html 标记或 web 控件的语法后,该页面第一次被访问时, asp.net 引擎会自动产生一个类。如果你是使用 code-behind 技术创建的 asp.net web 页,这个自动创建的类是继承自 code-behind 中的那个类(注意: code-behind 中的类必须直接或间接的继承自 System.Web.UI.Page 类)。如果你使用了 <script> 来创建你的页面,则这个自动创建的类将直接继承自 System.Web.UI.Page 。无论哪种情况,这个自动创建的类将和它的一个被编译的实例一起,存储在 WINDOWS /Microsoft.NET/Framework/version/Temporary ASP.NET Files 中。所以,当再次访问该页面是这个类不需要再次创建。
(译者笔记:讲 autogenerated class 存储在哪里)
The purpose of this autogenerated class is to programmatically create the page's control hierarchy. That is, the class is responsible for programmatically creating the Web controls specified in the page's HTML portion. This is done by translating the Web control syntax— <asp:WebControlName Prop1="Value1" ... /> into the class's programming language (C# or Microsoft® Visual Basic® .NET, most typically). In addition to the Web control syntax being converted into the appropriate code, the HTML markup present in the ASP.NET Web page's HTML portion is translated to Literal controls.
这个自动创建的类的作用是:自动的产生页面的 control hierarchy. 也就是说,这个类
负责创建 web 控件的清单。这个类的创建是通过将 web 控件的语法翻译成该类的编程语言来创建的。另外, web 控件的语法被转换为适当的代码的同时, html 标签被转换成了 Listeral 控件。(译者注:讲 autogenerated class 怎么产生的)
All ASP.NET server controls can have a parent control, along with a variable number of child controls. The System.Web.UI.Page class is derived from the base control class ( System.Web.UI.Control ), and therefore also can have a set of child controls. The top-level controls declared in an ASP.NET Web page's HTML portion are the direct children of the autogenerated Page class. Web controls can also be nested inside one another. For example, most ASP.NET Web pages contain a single server-side Web Form, with multiple Web controls inside the Web Form. The Web Form is an HTML control ( System.Web.UI.HtmlControls.HtmlForm ). Those Web controls inside the Web Form are children of the Web Form.
Since server controls can have children, and each of their children may have children, and so on, a control and its descendents form a tree of controls. This tree of controls is called the control hierarchy.
The root of the control hierarchy for an ASP.NET Web page is the Page -derived class that is autogenerated by the ASP.NET engine.
所有的 asp.net 服务器控件都有一个父控件,同时又有若干子控件。 System.Web.UI.Page 类是从控件基类 System.Web.UI.Control 派生来的,因此它能够有子控件。 Asp.net web 页的 html 中,最顶层的控件是 autogenerated Page 类的 direct chiledren 。一个 Web 控件可以嵌套在另一个 web 控件中。举例:大多数 asp.net web 页都有一个单独的服务器端 form ,同时有很多 web 控件在这个 web form 中。这个 web form 就是一个 html 控件( System.Web.UI.HtmlControls.HtmlForm )。在 web form 中的 web 控件就是这个 web form 的孩子。
因为服务器控件能够有孩子,并且它的每个孩子有可以有孩子,如此下去,一个控件和它的孩子们就形成了一棵树。这个控件树就被称作 control hierarchy
一个 asp.net web 页的 Control hierarchy 的根是一个 Page 类的子类,这个类是由 asp.net 引擎自动创建的。
(译者注:讲什么是页面控件层次结构 :control hierarchy
Whew ! Those last few paragraphs may have been a bit confusing, as this is not the easiest subject to discuss or digest. To clear out any potential confusion, let's look at a quick example. Imagine you have an ASP.NET Web page with the following HTML portion:
Copy Code
<html> <body> <h1>Welcome to my Homepage!</h1> <form runat="server"> What is your name? <asp:TextBox runat="server" ID="txtName"></asp:TextBox> <br />What is your gender? <asp:DropDownList runat="server" ID="ddlGender"> <asp:ListItem Select="True" Value="M">Male</asp:ListItem> <asp:ListItem Value="F">Female</asp:ListItem> <asp:ListItem Value="U">Undecided</asp:ListItem> </asp:DropDownList> <br /> <asp:Button runat="server" Text="Submit!"></asp:Button> </form> </body> </html>
When this page is first visited, a class will be autogenerated that contains code to programmatically build up the control hierarchy. The control hierarchy for this example can be seen in Figure 3
喔!刚刚那几段有点难以理解。为了弄清楚,我们来看一个例子。假想你有一个 asp.net web 页,且它有下面的 html 部分。当这个页面第一次被访问时,将会自动创建一类,这个类能够创建 control hierarchy 。本例的这个 control hierarchy 将在第 3 部分看到。
Figure 3. Control Hierarchy for sample page
(例页的 control hierarchy
This control hierarchy is then converted to code that is similar to the following:
这个 control hierarchy 会被转换成类似如下的代码
Copy Code
Page.Controls.Add( new LiteralControl(@"<html>/r/n<body>/r/n <h1>Welcome to my Homepage!</h1>/r/n")); HtmlForm Form1 = new HtmlForm(); Form1.ID = "Form1"; Form1.Method = "post"; Form1.Controls.Add( new LiteralControl("/r/nWhat is your name?/r/n")); TextBox TextBox1 = new TextBox(); TextBox1.ID = "txtName"; Form1.Controls.Add(TextBox1); Form1.Controls.Add( new LiteralControl("/r/n<br />What is your gender?/r/n")); DropDownList DropDownList1 = new DropDownList(); DropDownList1.ID = "ddlGender"; ListItem ListItem1 = new ListItem(); ListItem1.Selected = true; ListItem1.Value = "M"; ListItem1.Text = "Male"; DropDownList1.Items.Add(ListItem1); ListItem ListItem2 = new ListItem(); ListItem2.Value = "F"; ListItem2.Text = "Female"; DropDownList1.Items.Add(ListItem2); ListItem ListItem3 = new ListItem(); ListItem3.Value = "U"; ListItem3.Text = "Undecided"; DropDownList1.Items.Add(ListItem3); Form1.Controls.Add( new LiteralControl("/r/n<br /> /r/n")); Button Button1 = new Button(); Button1.Text = "Submit!"; Form1.Controls.Add(Button1); Form1.Controls.Add( new LiteralControl("/r/n</body>/r/n</html>")); Controls.Add(Form1);
Note    The C# source code above is not the precise code that is autogenerated by the ASP.NET engine. Rather, it's a cleaner and easier to read version of the autogenerated code. To see the full autogenerated code—which won't win any points for readability—navigate to the WINDOWS /Microsoft.NET/Framework/Version/Temporary ASP.NET Files folder and open one of the .cs or .vb files.
 
注意:上面的 C# 代码并不是 asp.net 引擎产生的实际代码。这仅是一个方便阅读的简化了的代码。完全自动产生的代码很难阅读。你可以从 WINDOWS/Microsoft.NET/Framework/Version/Temporary ASP.NET Files 文件夹中找到 .cs .vb 的文件,它们是完全自动产生的。
One thing to notice is that, when the control hierarchy is constructed, the properties that are explicitly set in the declarative syntax of the Web control are assigned in the code. (For example, the Button Web control has its Text property set to "Submit!" in the declarative syntax – Text="Submit!" – as well as in the autogenerated class— Button1.Text = "Submit!";
 
另一个要注意的事情是:一旦control hierarchy被创建了,web控件在声明语法中指定的属性的值将被写入control hierarchy中。(举例:在声明时设置了button web 控件text属性值为“submit”(Text=”submit”),那么在自动产生的类中会有如下代码button1.text=”submit”;
( 译者注:将 control hierarchy 的内容 )
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值