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
的内容
)