带你走进ASP.NET(3)

作者:飞鹰 来源:www.ASPCool.com 添加时间:2003年3月6日 字体:

1.2.2内容和代码分离 

   现在的网站建设通常要求开发人员做后台的程序设计,前面有专业的美工做界面设计。虽然有时候开发人员也会做些界面设计,但是通常都无法达到专业的要求。上面说过,在以前的ASP中,由于代码和HTML页面语言混杂在一起,这就使得网站的建设变得相当的困难。在ASP.NET中,微软使用代码后置很好的解决了这个问题。 

   我们现在建立一个HTML页面,如下: 

  <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" 

   AutoEventWireup="false" Inherits="AspCool.WebForm1" %> 

  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > 

  <HTML> 

   <HEAD> 

   <title>WebForm1</title> 

   <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0"> 

   <meta name="CODE_LANGUAGE" Content="C#"> 

   <meta name="vs_defaultClientScript" content="javascript"> 

   <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5";> 

   </HEAD> 

   <body MS_POSITIONING="GridLayout"> 

   <form id="Form1" method="post" runat="server"> 

   <FONT face="宋体"> 

   <asp:TextBox id="TextBox1" style="Z-INDEX: 101; LEFT: 47px; POSITION: absolute; TOP: 23px" runat="server"></asp:TextBox> 

   <asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 53px; POSITION: absolute; TOP: 76px" runat="server" Text="Button"></asp:Button></FONT> 

   </form> 

   </body> 

   

   

  从第一行中我们可以看出,该页面的后台代码全都在WebForm1.aspx.cs文件中。我们可以在这个文件中编写程序,如下所示: 

  using System; 

  using System.Collections; 

  using System.ComponentModel; 

  using System.Data; 

  using System.Drawing; 

  using System.Web; 

  using System.Web.SessionState; 

  using System.Web.UI; 

  using System.Web.UI.WebControls; 

  using System.Web.UI.HtmlControls; 

   

  namespace AspCool 

  { 

   /// <summary> 

   /// Summary description for WebForm1. 

   /// </summary> 

   public class WebForm1 : System.Web.UI.Page 

   { 

   protected System.Web.UI.WebControls.TextBox TextBox1; 

   protected System.Web.UI.WebControls.Button Button1; 

   

   private void Page_Load(object sender, System.EventArgs e) 

   { 

   // Put user code to initialize the page here 

   } 

   

   #region Web Form Designer generated code 

   override protected void OnInit(EventArgs e) 

   { 

   // 

   // CODEGEN: This call is required by the ASP.NET Web Form Designer. 

   // 

   InitializeComponent(); 

   base.OnInit(e); 

   } 

   

   /// <summary> 

   /// Required method for Designer support - do not modify 

   /// the contents of this method with the code editor. 

   /// </summary> 

   private void InitializeComponent() 

   { 

   this.Button1.Click += new System.EventHandler(this.Button1_Click); 

   this.Load += new System.EventHandler(this.Page_Load); 

   

   } 

   #endregion 

   

   private void Button1_Click(object sender, System.EventArgs e) 

   { 

   

   } 

   } 

  } 

  通过代码后置,开发人员可以直接修改.cs文件(在Visual Basic.NET中是.vb文件)。而页面设计人员可以修改HTML页面,这样就大大简化了网站的建设过程。 

  1.2.3 ASP.NET丰富的Web控件 

   ASP.NET的另外一个优点就是给我们提供了大量的丰富的Web控件。你可以在System.Web.UI.WebControls名字空间下找到各种各样的Web控件,这些控件中包括运行在服务端的from控件,例如:Button、TextBox等,同时也包括一些特殊用途的控件,如:广告轮换控件、日历控件,以及用户验证控件等。下面我们就具几个例子来看看如何使用这些控件。 

   

  1. 广告轮换控件<asp:adrotator> 

  广告轮换控件可以在网页上显示旋转的广告。广告链接信息保存在一个XML文件中,如ads.xml。 

   

  <?xml version="1.0" encoding="utf-8" ?> 

  <Advertisements> 

   

   <Ad> 

   <ImageUrl>http://www.aspcool.com/images/newaspcool.gif<;/ImageUrl> 

   <NavigateUrl>http://www.aspcool.com<;/NavigateUrl> 

   <AlternateText>Alt Text</AlternateText> 

   <Keyword>Computers</Keyword> 

   <Impressions>80</Impressions> 

   </Ad> 

   

   <Ad> 

   <ImageUrl>http://www.aspcool.com/images/chat.gif<;/ImageUrl> 

   <NavigateUrl>http://www.chaxiu.com<;/NavigateUrl> 

   <AlternateText>Alt Text</AlternateText> 

   <Keyword>Computers</Keyword> 

   <Impressions>80</Impressions> 

   </Ad> 

  </Advertisements> 

   

  我们现在在asp.net 页面中加上一个<asp:adrotator>,如下所示: 

  <HTML> 

   <HEAD> 

   <title>广告轮换程序</title> 

   </HEAD> 

   <body MS_POSITIONING="GridLayout"> 

   <form id="Form1" method="post" runat="server"> 

   <asp:AdRotator id="AdRotator1" runat="server" Width="468px" Height="60px" AdvertisementFile="ads.xml"></asp:AdRotator> 

   </form> 

   </body> 

  </HTML> 

  运行此程序,你就会得到一个旋转显示广告的页面了。 

   

  2. 日历控件<asp:calendar> 

  使用日历控件,我们可以很快生成一个日历的Web页面。代码如下: 

  <HTML> 

   <HEAD> 

   <title>广告轮换程序</title> 

   </HEAD> 

   <body MS_POSITIONING="GridLayout"> 

   <form id="Form1" method="post" runat="server"> 

   <asp:Calendar id="Calendar1" runat="server"></asp:Calendar> 

   </form> 

   </body> 

  </HTML> 

  在Visual Studio.NET中,你直接从工具栏中拖过来就可以使用了。 

   

  3. 验证控件 

  以前我们我们通过自己写javascript脚本来验证用户输入的信息。在ASP.NET中,只要我们使用验证控件这些代码也会自动生成。它能够向用户提示输入的错误信息。下面我就给大家列出这些验证控件: 

   

   

  控件名称 说明 

  RequiredFieldValidator 确保用户不跳过输入。 

  CompareValidator 使用比较运算符(小于、等于、大于等)将用户的输入与另一控件的常数值或属性值进行比较。 

  RangeValidator 检查用户的输入是否在指定的上下边界之间。 可以检查数字、字母或日期对内的范围。可以将边界表示为常数。 

  RegularExpressionValidator 检查输入是否与正则表达式定义的模式匹配。该验证类型允许检查可预知的字符序列,如社会保障号、电子邮件地址、电话号码、邮政编码等中的字符序列。 

  CustomValidator 使用您自己编写的验证逻辑检查用户的输入。该验证类型允许检查运行时导出的值。 

  ValidationSummary 以摘要的形式显示页上所有验证程序的验证错误。    

   

  好了,既然ASP.NET有如此多的优点,让我们现在就开始安装和使用ASP.NET吧! 

ppdesk