




已阅读5页,还剩6页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
附录 1 英文原文Introduction to ASP.NET Pages and Inline Code and Share10The ASP.NET Web Forms page framework is a scalable common language runtime programming model that can be used on the server to dynamically generate Web pages. Intended as a logical evolution of ASP (ASP.NET provides syntax compatibility with existing pages), the ASP.NET page framework has been specifically designed to address a number of key deficiencies in the previous model. In particular, it provides the ability to create and use reusable UI controls that can encapsulate common functionality and thus reduce the amount of code that a page developer has to write, the ability for developers to cleanly structure their page logic in an orderly fashion (not spaghetti code), and the ability for development tools to provide strong WYSIWYG design support for pages (existing classic ASP code is opaque to tools). This section of the QuickStart provides a high-level code walkthrough of some basic ASP.NET page features. Subsequent sections of the QuickStart drill down into more specific details.ASP.NET pages are text files with an .htm file name extension. Pages consist of code and markup and are dynamically compiled and executed on the server to produce a rendering to the requesting client browser (or device). They can be deployed throughout an IIS virtual root directory tree. When a browser client requests .htm resources, the ASP.NET runtime parses and compiles the target file into a .NET Framework class. This class can then be used to dynamically process incoming requests. (Note that the .htm file is compiled only the first time it is accessed; the compiled type instance is then reused across multiple requests). An ASP.NET page can be created simply by taking an existing HTML file and changing its file name extension to .htm (no modification of code is required). For example, the following sample demonstrates a simple HTML page that collects a users name and category preference and then performs a form postback to the originating page when a button is clicked: Important: Note that nothing happens yet when you click the Lookup button. This is because the .htm file contains only static HTML (no dynamic content). Thus, the same HTML is sent back to the client on each trip to the page, which results in a loss of the contents of the form fields (the text box and drop-down list) between requests. ASP.NET provides syntax compatibility with existing ASP pages. This includes support for code render blocks that can be intermixed with HTML content within an .htm file. These code blocks execute in a top-down manner at page render time. The below example demonstrates how render blocks can be used to loop over an HTML block (increasing the font size each time): Important: Unlike with ASP, the code used within the above blocks is actually compiled-not interpreted using a script engine. This results in improved runtime execution performance. ASP.NET page developers can utilize code blocks to dynamically modify HTML output much as they can today with ASP. For example, the following sample demonstrates how code blocks can be used to interpret results posted back from a client. Important: While code blocks provide a powerful way to custom manipulate the text output returned from an ASP.NET page, they do not provide a clean HTML programming model. As the sample above illustrates, developers using only code blocks must custom manage page state between round trips and custom interpret posted values. In addition to code and markup, ASP.NET pages can contain server controls, which are programmable server-side objects that typically represent a UI element in the page, such as a textbox or image. Server controls participate in the execution of the page and produce their own markup rendering to the client. The principle advantage of server controls is that they enable developers to get complex rendering and behaviors from simple building-block components, dramatically reducing the amount of code it takes to produce a dynamic Web page. Another advantage of server controls is that it is easy to customize their rendering or behavior. Server controls expose properties that can be set either declaratively (on the tag) or programmatically (in code). Server controls (and the page itself) also expose events that developers can handle to perform specific actions during the page execution or in response to a client-side action that posts the page back to the server (a postback). Server controls also simplify the problem of retaining state across round-trips to the server, automatically retaining their values across successive postbacks. Server controls are declared within an .htm file using custom tags or intrinsic HTML tags that contain a runat=server attribute value. Intrinsic HTML tags are handled by one of the controls in the System.Web.UI.HtmlControls namespace. Any tag that doesnt explicitly map to one of the controls is assigned the type of System.Web.UI.HtmlControls.HtmlGenericControl. Important: Note that these server controls automatically maintain any client-entered values between round trips to the server. This control state is not stored on the server (it is instead stored within an form field that is round-tripped between requests). Note also that no client-side script is required. In addition to supporting standard HTML input controls, ASP.NET enables developers to utilize richer custom controls on their pages. For example, the following sample demonstrates how the control can be used to dynamically display rotating ads on a page. Each ASP.NET server control is capable of exposing an object model containing properties, methods, and events. ASP.NET developers can use this object model to cleanly modify and interact with the page. Note, however, how much cleaner and easier the code is in this new server-control-based version. As we will see later in the tutorial, the ASP.NET page Framework also exposes a variety of page-level events that you can handle to write code to execute a specific time during the processing of the page. Examples of these events are Page_Load and Page_Render.The example below demonstrates a simple ASP.NET page with three server controls, a TextBox, Button, and a Label. Initially these controls just render their HTML form equivalents. However, when a value is typed in the TextBox and the Button is clicked on the client, the page posts back to the server and the page handles this click event in the code of the page, dynamically updating the Text property of the Label control. The page then re-renders to reflect the updated text. This simple example demonstrates the basic mechanics behind the server control model that has made ASP.NET one of the easiest Web programming models to learn and master.Note that in the preceding example the event handler for the Button was located between tags in the same page containing the server controls. ASP.NET calls this type of page programming code-inline, and it is very useful when you want to maintain your code and presentation logic in a single file. However, ASP.NET also supports another way to factor your code and presentation content, called the code-behind model. When using code-behind, the code for handling events is located in a physically separate file from the page that contains server controls and markup. This clear delineation between code and content is useful when you need to maintain these separately, such as when more than one person is involved in creating the application. It is often common in group projects to have designers working on the UI portions of an application while developers work on the behavior or code. The code-behind model is well-suited to that environment.ASP.NET 2.0 introduces an improved runtime for code-behind pages that simplifies the connections between the page and code. In this new code-behind model, the page is declared as a partial class, which enables both the page and code files to be compiled into a single class at runtime. The page code refers to the code-behind file in the CodeFile attribute of the directive, specifying the class name in the Inherits attribute. Note that members of the code behind class must be either public or protected (they cannot be private).The advantage of the simplified code-behind model over previous versions is that you do not need to maintain separate declarations of server control variables in the code-behind class. Using partial classes (new in 2.0) allows the server control IDs of the ASPX page to be accessed directly in the code-behind file. This greatly simplifies the maintenance of code-behind pages.Although you can place code inside each page within your site (using the inline or code-behind separation models described in the previous section), there are times when you will want to share code across several pages in your site. It would be inefficient and difficult to maintain this code by copying it to every page that needs it. Fortunately, ASP.NET provides several convenient ways to make code accessible to all pages in an application.Just as pages can be compiled dynamically at runtime, so can arbitrary code files (for example .cs or .vb files). ASP.NET 2.0 introduces the App_Code directory, which can contain standalone files that contain code to be shared across several pages in your application. Unlike ASP.NET 1.x, which required these files to be precompiled to the Bin directory, any code files in the App_Code directory will be dynamically compiled at runtime and made available to the application. It is possible to place files of more than one language under the App_Code directory, provided they are partitioned in subdirectories (registered with a particular language in Web.config). The example below demonstrates using the App_Code directory to contain a single class file called from the page.By default, the App_Code directory can only contain files of the same language. However, you may partition the App_Code directory into subdirectories (each containing files of the same language) in order to contain multiple languages under the App_Code directory. To do this, you need to register each subdirectory in the Web.config file for the application. Supported in ASP.NET version 1, the Bin directory is like the Code directory, except it can contain precompiled assemblies. This is useful when you need to use code that is possibly written by someone other than yourself, where you dont have access to the source code (VB or C# file) but you have a compiled DLL instead. Simply place the assembly in the Bin directory to make it available to your site. By default, all assemblies in the Bin directory are automatically loaded in the app and made accessibe to pages. You may need to Import specific namespaces from assemblies in the Bin directory using the Import directive at the top of the page. The .NET Framework 2.0 includes a number of assemblies that represent the various parts of the Framework. These assemblies are stored in the global assembly cache, which is a versioned repository of assemblies made available to all applications on the machine (not just a specific application, as is the case with Bin and App_Code). Several assemblies in the Framework are automatically made available to ASP.NET applications. You can register additional assemblies by registration in a Web.config file in your application. 附录 2 中文译文ASP.NET介绍以及内联代码和共享ASP.NET Web 窗体页框架是一种可用于在服务器上动态生成网页的可伸缩公共语言运行库编程模型。作为ASP的继承和发展,ASP.NET 页框架消除了以前ASP中存在的缺陷。尤其是,它提供了创建和使用可封装常用功能的可重用用户界面控件的能力,从而减少了页开发人员编写代码的数量,它还为开发人员提供了清晰、有序地构造页面的能力,以及可使开发工具真正做到所见即所得的功能。本部分的快速入门提供对某些基本 ASP.NET 页功能的高级代码演练,后面部分的快速入门将深入介绍更具体的细节。ASP.NET 页是采用 .aspx 文件扩展名的文本文件。页由代码和HTML组成,并在服务器上动态编译和执行以呈现给发出请求的客户端浏览器。它们放在IIS服务器上的虚拟目录中。当浏览器客户端请求 .aspx 资源时,ASP.NET 运行库会对目标文件进行分析并将其编译为 .NET Framework 类。然后,可以使用此类动态处理传入的请求。只需获取现有 HTML 文件并将该文件的文件扩展名更改为 .aspx,就可以创建一个 ASP.NET 页。例如,下面的示例演示了一个简单 HTML 页,它收集用户名和类别首选项,然后在单击某一按钮时执行窗体回发,发送至发起请求的页。重要事项:注意在单击“Lookup”(查找)按钮时,不会发生任何操作。这是因为 .aspx 文件只包含静态 HTML(不包含动态内容)。因此,相同的 HTML 在每次发送到页时都会被发送回客户端,这会导致在请求之间窗体字段(文本框和下拉列表)的内容丢失。 ASP.NET 提供了与现有 ASP 页的语法兼容性。这包括对 代码呈现块的支持,这些块可以与 .aspx 文件中的 HTML 内容混用。在呈现页时,这些代码块以从上至下的方式执行。重要事项:与 ASP 不同,上述 块中使用的代码实际上是使用脚本引擎编译的,而不是解释。这可以提高运行时执行性能。 ASP.NET 页开发人员可利用 代码块动态修改 HTML 输出,就好像目前可使用 ASP 进行修改一样。例如,下面的示例演示如何使用 代码块解释从客户端发回的请求。重要事项:尽管 代码块提供了一种对从 ASP.NET 页返回的文本输出进行自定义操作的强大方法,但这些代码块未提供一种清晰的 HTML 编程模型。正如上述示例所阐述的那样,只使用 代码块的开发人员必须自定义管理往返过程之间的页状态并自定义解释发送的值。 除了代码和标记之外,ASP.NET 页还可以包含服务器控件,这些控件是可编程的服务器端对象,通常表示页中的用户界面元素,如文本框或图像等。服务器控件参与页的执行,并生成它们自己的标记呈现给客户端。服务器控件的主要优点在于它们使开发人员可以从简单的构造块组件获取复杂的呈现和行为,从而大幅度减少了生成动态网页所需的代码量。服务器控件的另一个优点在于可以很容易地自定义其呈现或行为。服务器控件公开可以声明方式(通过标记)或编程方式(通过代码)设置的属性。服务器控件(和页本身)还公开了一些事件,开发人员可以处理这些事件以在页执行期间执行特定的操作或响应将页发回服务器的客户端操作(“回发”)。此外,服务器控件还简化了在往返于服务器的过程中保留状态的问题,自动在连续回发之间保留其值。 服务器控件在 .aspx 文件中是使用自定义标记或包含 runat=server 属性值的内部 HTML 标记声明的。内部 HTML 标记由 System.Web.UI.HtmlControls 命名空间中的某个控件处理。未显式映射到这些控件之一的任何标记都将被指定为 System.Web.UI.HtmlControls.HtmlGenericControl 类型。 下面的示例使用以下四个服务器控件:、 和 。在运行时,这些服务器控件将自动生成 HTML 内容。 重要事项:注意,在到服务器的往返过程之间,这些服务器控件自动保留客户端输入的任何值。此控件状态不存储在服务器上(而是存储在往返于请求之间的 窗体字段中)。另外,还须注意不需要客户端脚本。 除支持标准 HTML 输入控件之外,ASP.NET 还使开发人员能够在他们的页上利用更丰富的自定义控件。例如,下面的示例演示如何使用 控件在页上动态显示循环广告。重要事项:所有内置服务器控件的详细列表可在本快速入门的“控件参考” 部分中找到。 每个 ASP.NET 服务器控件都可以公开包含属性、方法和事件的对象模型。ASP.NET 开发人员可以使用此对象模型清晰地修改页以及与页进行交互。 下面的示例演示 ASP.NET 页开发人员可如何处理 控件中的 OnClick 事件以操作 控件的 Text 属性。 ASP.NET 提供了可以组织页中代码的两种方法。 下面的代码示例演示一个包含以下三个服务器控件的简单 ASP.NET 页:TextBox、Button 和 Label。最初,这些控件只呈现其 HTML 窗体等效项。但是,当客户端在 TextBox 中键入了一个值并单击了 Button 后,该页会回发到服务器,然后该页使用页中的代码处理此单击事件,动态地更新 Label 控件的 Text 属性。然后,该页将重新呈现以反映更新后的文本。此简单示例演示了服务器控件模型内在的基本机制,该模型使 ASP.NET 成为最容易学习和掌握的 Web 编程模型之一。 ASP
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 湖北省武汉市青山区5月2026届九年级化学第一学期期中质量检测试题含解析
- 2026届三门峡市重点中学化学九年级第一学期期中联考试题含解析
- 2026届四川省绵阳市绵阳外国语学校英语九年级第一学期期末经典试题含解析
- 离婚协议范本:财产分割、子女抚养及债务偿还方案
- 高端商务楼宇物业管理与客户关系维护合同
- 竞业禁止协议赔偿金额界定与劳动者权益保障
- 跨界融合的私立学校校长聘用与管理合同
- 税务筹划与税收筹划培训顾问服务协议
- 离婚时共同房产处置及居住权分配协议公证模板
- 离婚债务承担与财产分割及子女抚养费用分担协议
- 危险性较大分部分项工程安全专项施工方案专家论证审查表
- 城乡规划管理与法规系列讲座城乡规划的监督检查
- 惠东渔歌的历史流变
- 新汉语水平考试HSK级写作解题攻略专题培训课件
- 学习提高阅读速度的方法 课件
- 第一单元知识盘点(含字词、佳句、感知、考点) 四年级语文上册 (部编版有答案)
- 钻井工程钻柱课件
- 小学硬笔书法课教案(1-30节)
- 周口市医疗保障门诊特定药品保险申请表
- 校园物业考评表
- 爆破作业人员培训考核题库
评论
0/150
提交评论