Building a Web Application->Introduction to ASP.NET Pages

The 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//具体的细节.

Writing Your First ASP.NET Page

ASP.NET pages are text files with an .aspx file name extension//.aspx文件扩展名. 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//iis的虚拟的根目录树. When a browser client requests//浏览器客户端请求 .aspx 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 .aspx 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 .aspx (no modification //修改of code is required). For example, the following sample demonstrates //举例说明a simple HTML page that collects a user's name and category//种类,类别 preference//偏爱 and then performs //执行,表演,做a form postback//返回 to the originating page when a button is clicked:

C# Intro1.aspx
<html>
   
<head>
      
<link rel="stylesheet" href="intro.css">
   
</head>

   
<body>

       
<center>

       
<form action="intro1_cs.aspx" method="post">

           
<h3> Name: <input id="Name" type=text>

           Category:  
<select id="Category" size=1>
                          
<option>psychology</option>
                          
<option>business</option>
                          
<option>popular_comp</option>
                      
</select>

           
<input type=submit value="Lookup">
           
</h3>
       
</form>

       
</center>

   
</body>
</html>


Important: Note that nothing happens yet when you click the Lookup//检查(查找) button. This is because the .aspx 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.

Adding Simple Code to a Page

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 .aspx 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):

C# Intro2.aspx
<%@ Page Language="C#"%>

<html>
   
<head>
      
<link rel="stylesheet"href="intro.css">
   
</head>

   
<body>

       
<center>

       
<form action="intro2_cs.aspx" method="post">

           
<h3> Name: <input id="Name" type=text>

           Category:  
<select id="Category" size=1>
                          
<option>psychology</option>
                          
<option>business</option>
                          
<option>popular_comp</option>
                      
</select>

           
</h3>

           
<input type=submit value="Lookup">

           
<p>

           
<% for (int i=0; i <8; i++%>
              
<font size="<%=i%>"> Welcome to ASP.NET </font> <br>
           
<% }
%>

       
</form>

       
</center>

   
</body>
</html>


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.

C# Intro3.aspx
<%@ Page Language="C#" %>

<html>
   
<head>
      
<link rel="stylesheet"href="intro.css">
   
</head>

   
<body>

       
<center>

       
<form action="intro3_cs.aspx">

           
<h3> Name: <input name="Name" type=text value="<%=HttpUtility.HtmlEncode(Request.QueryString["Name"])%>">

           Category:  
<select name="Category" size=1>

                         
<%
                             String [] values 
= "psychology""business""popular_comp" };

                             
for (int i=0; i<values.Length; i++{
                          
%>

                                
<option <% if (Request.QueryString["Category"== values[i]) { Response.Write("selected"); } %>>
                                   
<%=values[i]%>
                                
</option>

                          
<% }
 %>

                      
</select>

           
</h3>

           
<input type=submit name="Lookup" value="Lookup">

           
<p>

           
<% if (Request.QueryString["Lookup"!= null%>

              Hi 
<%=HttpUtility.HtmlEncode(Request.QueryString["Name"]) %>, you selected: <%=HttpUtility.HtmlEncode(Request.QueryString["Category"]) %>

           
<% }
 %>

       
</form>

       
</center>

   
</body>
</html>


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.

Introduction to ASP.NET Server Controls

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 .aspx 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 doesn't explicitly //明白的,明确的map to one of the controls is assigned the type of System.Web.UI.HtmlControls.HtmlGenericControl.

The following sample uses four server controls: <form runat=server>, <asp:textbox runat=server>, <asp:dropdownlist runat=server>, and <asp:button runat=server>. At run time these server controls automatically generate //产生HTML content.

C# Intro4.aspx
<%@ Page Language="C#"%>

<html>
   
<head>
      
<link rel="stylesheet"href="intro.css">
   
</head>

   
<body>

       
<center>

       
<form action="intro4_cs.aspx" method="post" runat=server>

           
<h3> Name: <asp:textbox id="Name" runat="server"/>

           Category:  
<asp:dropdownlist id="Category" runat=server>
                         
<asp:listitem>psychology</asp:listitem>
                         
<asp:listitem>business</asp:listitem>
                         
<asp:listitem>popular_comp</asp:listitem>
                      
</asp:dropdownlist>

           
</h3>

           
<asp:button text="Lookup" runat="server"/>

       
</form>

       
</center>

   
</body>
</html>


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 <input type="hidden"> 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,//标准的HTML输入控件 ASP.NET enables developers to utilize//利用 richer //更加丰富的custom controls on their pages. For example, the following sample demonstrates//举例说明 how the <asp:adrotator> control can be used to dynamically display rotating ads //滚动的广告on a page.

C# Intro5.aspx
<%@ Page Language="C#"%>

<html>
   
<head>
      
<link rel="stylesheet"href="intro.css">
   
</head>

   
<body>

       
<center>

       
<form action="intro5_cs.aspx" method="post" runat="server">

           
<asp:adrotator AdvertisementFile="ads.xml" BorderColor="black" BorderWidth=1 runat="server"/>

           
<h3> Name: <asp:textbox id="Name" runat="server"/>

           Category:  
<asp:dropdownlist id="Category" runat=server>
                         
<asp:listitem>psychology</asp:listitem>
                         
<asp:listitem>business</asp:listitem>
                         
<asp:listitem>popular_comp</asp:listitem>
                      
</asp:dropdownlist>

           
</h3>

           
<asp:button text="Lookup" runat="server"/>

       
</form>

       
</center>

   
</body>
</html>


Handling Server Control Events//处理服务器端控件事件

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.

The following example demonstrates how an ASP.NET page developer can handle the OnClick event //单击事件from the <asp:button runat=server> control to manipulate //处理the Text property of the <asp:label runat=server> control.

C# Intro6.aspx
<html>
   
<head>
      
<link rel="stylesheet"href="intro.css">
   
</head>

   
<script language="C#" runat=server>

       
void SubmitBtn_Click(Object sender, EventArgs e) {
           Message.Text 
= "Hi " + HttpUtility.HtmlEncode(Name.Text) + ", you selected: " + Category.SelectedItem;
       }


   
</script>

   
<body>

       
<center>

       
<form action="intro6_cs.aspx" method="post" runat="server">

           
<asp:adrotator AdvertisementFile="ads.xml" BorderColor="black" BorderWidth=1 runat="server"/>

           
<h3> Name: <asp:textbox id="Name" runat="server"/>

           Category:  
<asp:dropdownlist id="Category" runat=server>
                         
<asp:listitem>psychology</asp:listitem>
                         
<asp:listitem>business</asp:listitem>
                         
<asp:listitem>popular_comp</asp:listitem>
                      
</asp:dropdownlist>

           
</h3>

           
<asp:button text="Lookup" OnClick="SubmitBtn_Click" runat="server"/>

           
<p>

           
<asp:label id="Message" runat="server"/>

       
</form>

       
</center>

   
</body>
</html>


This simple sample is functionally equivalent //相等的,等价的to the "Intro3" sample demonstrated earlier in this section//在这一部分. 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//页面展示.

posted on 2007-04-25 11:09  改变热爱  阅读(327)  评论(0)    收藏  举报

导航