陈鹏-外文翻译.doc_第1页
陈鹏-外文翻译.doc_第2页
陈鹏-外文翻译.doc_第3页
陈鹏-外文翻译.doc_第4页
陈鹏-外文翻译.doc_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

外文翻译城市公交查询系统的设计与实现学 生 姓 名: 陈鹏 指导教师: 王萍 副教授 合作指导教师: 专业名称: 计算机科学与技术 所在学院: 大连水产学院 2009 年6月原文摘自:lvar Jacobson.Object-Oriented Software Engineering.第1版.北京:人民邮电出版社,2005.10外文资料原文ASP.NET PAGE OBJECT MODEL:Summary: Learn about the eventing model built around ASP.NET Web pages and the various stages that a Web page experiences on its way to HTML. The ASP.NET HTTP run time governs the pipeline of objects that transform the requested URL into a living instance of a page class first, and into plain HTML text next. Discover the events that characterize the lifecycle of a page and how control and page authors can intervene to alter the standard behavior. (6 printed pages)Introduction: Each request for a Microsoft ASP.NET page that hits Microsoft Internet Information Services (IIS) is handed over to the ASP.NET HTTP pipeline. The HTTP pipeline is a chain of managed objects that sequentially process the request and make the transition from a URL to plain HTML text happen. The entry point of the HTTP pipeline is the HttpRuntime class. The ASP.NET infrastructure creates one instance of this class per each AppDomain hosted within the worker process (remember that the worker process maintains one distinct AppDomain per each ASP.NET application currently running).The HttpRuntime class picks up an HttpApplication object from an internal pool and sets it to work on the request. The main task accomplished by the HTTP application manager is finding out the class that will actually handle the request. When the request is for an .aspx resource, the handler is a page handlernamely, an instance of a class that inherits from Page. The association between types of resources and types of handlers is stored in the configuration file of the application. More exactly, the default set of mappings is defined in the section of the machine.config file. However, the application can customize the list of its own HTTP handlers in the local web.config file. The line below illustrates the code that defines the HTTP handler for .aspx resources.An extension can be associated with a handler class, or more in general, with a handler factory class. In all cases, the HttpApplication object in charge for the request gets an object that implements the IHttpHandler interface. If the association resource/class is resolved in terms of a HTTP handler, then the returned class will implement the interface directly. If the resource is bound to a handler factory, an extra step is necessary. A handler factory class implements the IHttpHandlerFactory interface whose GetHandler method will return an IHttpHandler-based object.How can the HTTP run time close the circle and process the page request? The IHttpHandler interface features the ProcessRequest method. By calling this method on the object that represents the requested page, the ASP.NET infrastructure starts the process that will generate the output for the browser.The Real Page ClassThe type of the HTTP handler for a particular page depends on the URL. The first time the URL is invoked, a new class is composed and dynamically compiled to an assembly. The source code of the class is the outcome of a parsing process that examines the .aspx sources. The class is defined as part of the namespace ASP and is given a name that mimics the original URL. For example, if the URL endpoint is page.aspx, the name of the class is ASP.Page_aspx. The class name, though, can be programmatically controlled by setting the ClassName attribute in the Page directive.The base class for the HTTP handler is Page. This class defines the minimum set of methods and properties shared by all page handlers. The Page class implements the IHttpHandler interface.Under a couple of circumstances, the base class for the actual handler is not Page but a different class. This happens, for example, if code-behind is used. Code-behind is a development technique that insulates the code necessary to a page into a separate C# or Microsoft Visual Basic.NET class. The code of a page is the set of event handlers and helper methods that actually create the behavior of the page. This code can be defined inline using the tag or placed in an external classthe code-behind class. A code-behind class is a class that inherits from Page and specializes it with extra methods. When specified, the code-behind class is used as the base class for the HTTP handler.The other situation in which the HTTP handler is not based on Page is when the configuration file of the application contains a redefinition for the PageBaseType attribute in the section.The PageBaseType attribute indicates the type and the assembly that contains the base class for page handlers. Derived from Page, this class can automatically endow handlers with a custom and extended set of methods and properties.The Page LifecycleOnce the HTTP page handler class is fully identified, the ASP.NET run time calls the handlers ProcessRequest method to process the request. Normally, there is no need to change the implementation of the method as it is provided by the Page class.This implementation begins by calling the method FrameworkInitialize, which builds the controls tree for the page. The method is a protected and virtual member of the TemplateControl classthe class from which Page itself derives. Any dynamically generated handler for an .aspx resource overrides FrameworkInitialize. In this method, the whole control tree for the page is built.Next, ProcessRequest makes the page transit various phases: initialization, loading of view state information and postback data, loading of the pages user code and execution of postback server-side events. After that, the page enters in rendering mode: the updated view state is collected; the HTML code is generated and then sent to the output console. Finally, the page is unloaded and the request is considered completely served.During the various phases, the page fires a few events that Web controls and user-defined code can intercept and handle. Some of these events are specific for embedded controls and subsequently cant be handled at the level of the .aspx code.A page that wants to handle a certain event should explicitly register an appropriate handler. However, for backward compatibility with the earlier Visual Basic programming style, ASP.NET also supports a form of implicit event hooking. By default, the page tries to match special method names with events; if a match is found, the method is considered a handler for the event. ASP.NET provides special recognition of six method names. They are Page_Init, Page_Load, Page_DataBind, Page_PreRender, and Page_Unload. These methods are treated as handlers for the corresponding events exposed by the Page class. The HTTP run time will automatically bind these methods to page events saving developers from having to write the necessary glue code. For example, the method named Page_Load is wired to the pages Load event as if the following code was written.this.Load += new EventHandler(this.Page_Load);The automatic recognition of special names is a behavior under the control of the AutoEventWireup attribute of the Page directive. If the attribute is set to false, any applications that wish to handle an event need to connect explicitly to the page event. Pages that dont use automatic event wire-up will get a slight performance boost by not having(51-aspx) to do the extra work of matching names and events. You should note that all Microsoft Visual Studio .NET projects are created with the AutoEventWireup attribute disabled. However, the default setting for the attribute is true, meaning that methods such as Page_Load are recognized and bound to the associated event.The execution of a page consists of a sequence of stages listed in the following table and is characterized by application-level events and/or protected, overridable methods.Table 1. Key events in the life of an ASP.NET pageStagePage EventOverridable methodPage initializationInitView stateloadingLoadViewStatePostback data processingLoadPostData method in any control that implements the IPostBackDataHandler interfacePage loadingLoadPostback change notificationRaise51PosAspxtDataChangedEvent method in any control that implements the IPostBackDataHandler interfacePostback event handlingAny postback event defined by controlsRaisePostBackEvent method in any control that implements the IPostBackEventHandler interface Page pre-rendering phasePreRenderView state savingSaveViewStatePage renderingRenderPage unloadingUnloadSome of the stages listed above are not visible at the page level and affect only authors of server controls and developers who happen to create a class derived from Page. Init, Load, PreRender, Unload, plus all postback events defined by embedded controls are the only signals of life that a page sends to the external world.Stages of ExecutionThe first stage in the page lifecycle is the initialization. This stage is characterized by the Init event, which fires to the application after the pages control tree has been successfully created. In other words, when the Init event arrives, all the controls statically declared in the .aspx source file have been instantiated and hold their default values. Controls can hook up the Init event to initialize any settings that will be needed during the lifetime of the incoming Web request. For example, at this time controls can load external template files or set up the handler for the events. You should notice that no view state information is available for use yet.Immediately after initialization, the page framework loads the view state for the page. The view state is a collection of name/value pairs, where controls and the page itself store any information that must be persistent across Web requests. The view state represents the call context of the page. Typically, it contains the state of the controls the last time the page was processed on the server. The view state is empty the first time the page is requested in the session. By default, the view state is stored in a hidden field silently added to the page. The name of this field is _VIEWSTATE. By overriding the LoadViewState methoda protected overridable method on the Control classcomponent developers can control how the view state is restored and how its contents are mapped to the internal state.Methods like LoadPageStateFromPersistenceMedium and its counterpart SavePageStateToPersistenceMedium can be used to load and save the view state to an alternative storage mediumfor example, Session, databases, or a server-side file. Unlike LoadViewState, the aforementioned methods are available only in classes derived from Page.Once the view state has been restored, the controls in the page tree are in the same state they were the last time the page was rendered to the browser. The next step consists of updating their state to incorporate client-side changes. The postback data-processing stage gives controls a chance to update their state so that it accurately reflects the state of the corresponding HTML element on the client. For example, a server TextBox control has its HTML counterpart in an element. In the postback data stage, the TextBox control will retrieve the current value of tag and use it to refresh its internal state. Each control is responsible for extracting values from posted data and updating some of its properties. The TextBox control will update its Text property whereas the CheckBox control will refresh its Checked property. The match between a server control and a HTML element is found on the ID of both.At the end of the postback data processing stage, all controls in the page reflect the previous state updated with changes entered on the client. At this point, the Load event is fired to the page.There might be controls in the page that need to accomplish certain tasks if a sensitive property is modified across two different requests. For example, if the text of a textbox control is modified on the client, the control fires the TextChanged event. Each control can take the decision to fire an appropriate event if one or more of its properties are modified with the values coming from the client. Controls for which these changes are critical implement the IPostBackDataHandler interface, whose LoadPostData method is invoked immediately after the Load event. By coding the LoadPostData method, a control verifies if any critical change has occurred since last request and fires its own change event.The key event in the lifecycle of a page is when it is called to execute the server-side code associated with an event triggered on the client. When the user clicks a button, the page posts back. The collection of posted values contains the ID of the button that started the whole operation. If the control is known to implement the IPostBackEventHandler interface (buttons and link buttons will do), the page framework calls the RaisePostBackEvent method. What this method does depends on the type of the control. With regard to buttons and link buttons, the method looks up for a Click event handler and runs the associated delegate.After handling the postback event, the page prepares for rendering. This stage is signaled by the Pretender event. This is a good time for controls to perform any last minute update operations that need to take place immediately before the view state is saved and the output rendered. The next state is SaveViewState, in which all controls and the page itself are invited to flush the contents of their own ViewState collection. The resultant view state is then serialized, hashed, Base64 encoded, and associated with the _VIEWSTATE hidden field.The rendering mechanism of individual controls can be altered by overriding the Render method. The method takes an HTML writer object and uses it to accumulate all HTML text to be generated for the control. The default implementation of the Render method for the Page class consists of a recursive call to all constituent controls. For each control the page calls the Render method and caches the HTML output.The final sign of life of a page is the Unload event that arrives just before the page object is dismissed. In this event you should release any critical resource you might have (for example, files, graphical objects, database connections).Finally, after this event the browser receives the HTTP response packet and displays the page.SummaryThe ASP.NET page object model is particularly innovative because of the eventing mechanism. A Web page is composed of controls that both produce a rich HTML-based user interface and interact with the user through events. Setting up an eventing model in the context of Web applications is challenging. Its amazing to see that client-side generated events are resolved with server-side code, and the output of this is visible as the same HTML page, only properly modified.To make sense of this model it is important to understand the various stages in the page lifecycle and how the page object is instantiated and used by the HTTP run time.译成中文:ASP.NET 页面对象模型:摘要:了解围绕 ASP.NET Web 页构建的事件模型,以及一个 Web 页面在其转变为 HTML 的历程中的各个阶段。ASP.NET HTTP 运行时控制对象管线,对象管线首先将所请求的 URL 转换为一个页面类的活动实例,然后将其转换为普通 HTML 文本。本文将探讨一个页面的生存周期中的各个特征事件,并了解控件和页面编写者如何介入其中以改变其标准行为。简介:Microsoft Internet 信息服务 (IIS) 所收到的对某 Microsoft ASP.NET 页面的每个请求都被移交给 ASP.NET HTTP 管线。HTTP 管线由一系列托管对象组成,这些对象按顺序处理该请求,并完成从 URL 到普通 HTML 文本的转换。HTTP 管线的入口点是 HttpRuntime 类。ASP.NET 基础结构为辅助进程中所承载的每个 AppDomain 创建此类的一个实例(请注意,该辅助进程为当前正在运行的每个 ASP.NET 应用程序维护一个不同的 AppDomain)。HttpRuntime 类从内部池中选取一个 HttpApplication 对象,并让其处理该请求。HTTP 应用程序管理器所完成的主要任务就是找出将实际处理该请求的类。如果请求 .aspx 资源,则处理程序就是一个页面处理程序 即某个继承自 Page 的类的一个实例。资源类型和处理程序类型之间的关联关系存储于该应用程序的配置文件中。更准确地说,在 machine.config 文件的 部分中定义默认的一组映射关系。然而,应用程序也可以在本地的 web.config 文件中自定义自己的 HTTP 处理程序列表。下面的程序行举例说明了定义用于 .aspx 资源的 HTTP 处理程序的代码。扩展名可关联到一个处理程序类,或者更普遍地关联到一个处理程序工厂 (handler factory) 类。在所有情况下,负责处理请求的 HttpApplication 对象都会获得一个实现 IHttpHandler 接口的对象。如果根据 HTTP 处理程序来解析关联资源/类,那么所返回的类将直接实现该接口。如果资源绑定到处理程序工厂,则需要另外一个步骤。处理程序工厂类实现 IHttpHandlerFactory 接口,而该接口的 GetHandler 方法返回一个基于 IHttpHandler 的对象。HTTP 运行时如何能完成整个循环并处理页面请求呢?IHttpHandler 接口特别提供了 ProcessRequest 方法。通过对代表所请求页面的对象调用此方法,ASP.NET 基础结构启动相应过程,从而针对浏览器生成输出。真正的 Page 类特定页面的 HTTP 处理程序类型取决于 URL。当首次调用 URL 时,将构建一个新类并将该类动态地编译成一个程序集。用于检查 .aspx 来源的语法分析过程的输出结果就是该类的源代码。该类被定义为 ASP 命名空间的一部分,并被赋予一个与原始 URL 相似的名称。例如,如果 URL 终结点是 page.aspx,则类名称为 ASP.Page_aspx。但是,也可通过编程设置 Page 指令的 ClassName 属性来控制类的名称。HTTP 处理程序的基类是 Page。此类定义了所有页面处理程序所共享的方法和属性的最小集合。Page 类中实现 IHttpHandler 接口。在某些情况下,实际处理程序的基类并非 Page,而是一个不同的类。例如,如果使用了代码隐藏,就会出现这种情况。代码隐藏是一种开发方法,它将页面所需的代码封装到一个单独的 C# 或 Microsoft Visual Basic.NET 类中。页面的代码就是一组事件处理程序和帮助器方法,用以实际创建该页面的行为。可以利用 标记将这种代码定义为内联代码,或者也可将其放到一个外部类 代码隐藏类中。代码隐藏类是一种继承自 Page 的类,但这种类具有一些额外的方法因而比较特殊。如果指定,代码隐藏类就用作 HTTP 处理程序的基类。还有一种情况,即当应用程序配置文件的 部分中重新定义了 PageBaseType 属性时,HTTP 处理程序也不是基于 Page 的。PageBaseType 属性指出了包含页面处理程序的基类的类型以及程序集。派生自 Page 的这个类可自动给处理程序赋予一组自定义和扩展的方法和属性。页面生存周期一旦完全确定 HTTP 页面处理程序类,ASP.NET 运行时就调用该处理程序的 ProcessRequest 方法以处理请求。通常情况下,无需更改此方法的实现方式,因为它是由 Page 类提供的。此实现方法一开始就调用 FrameworkInitialize 方法,以此建立页面的控件树。此方法是 TemplateControl 类(Page 类本身就是从该类派生出来的)的一个受保护的虚拟成员。任何针对 .aspx 资源而动态生成的处理程序都重写 FrameworkInitialize。在此方法中,该页面的完整控件树得以构建。接下来,ProcessRequest 使该页面经历若干阶段:初始化,加载视图状态信息和回发数据,加载页面的用户代码并执行回发服务器端事件。随后,该页面进入呈现模式:收集更新后的视图状态;生成

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论