带你走进ASP.NET(3)

发布时间:2013-09-19 11:33:46来源:阅读(1225)

    1.2.2内容和代码分离 
       现在的网站建设通常要求开发人员做后台的程序设计,前面有专业的美工做界面设计。虽然有时候开发人员也会做些界面设计,但是通常都无法达到专业的要求。上面说过,在以前的ASP中,由于代码和HTML页面语言混杂在一起,这就使得网站的建设变得相当的困难。在ASP.net中,微软使用代码后置很好的解决了这个问题。 
       我们现在建立一个HTML页面,如下: 
      <%@ Page language="c#" Codebehind="WebForm1.ASPx.cs" 
       AutoEventWireup="false" Inherits="ASPCool.WebForm1" %> 
       
       
        
       WebForm1 
        
        
        
        
        
        
        
        
        
        
        
        
       
       
      从第一行中我们可以看出,该页面的后台代码全都在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 description for WebForm1. 
       /// 
     
       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); 
       } 
       
       ///  
       /// Required method for Designer support - do not modify 
       /// the contents of this method with the code editor. 
       /// 
     
       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. 广告轮换控件 
      广告轮换控件可以在网页上显示旋转的广告。广告链接信息保存在一个xml文件中,如ads.xml。 
       
       
       
       
        
       http://www.ASPcool.com/images/newaspcool.gif 
       http://www.ASPcool.com 
       Alt Text 
       Computers 
       80 
       
     
       
        
       http://www.ASPcool.com/images/chat.gif 
       http://www.chaxiu.com 
       Alt Text 
       Computers 
       80 
       
     
      
     
       
      我们现在在ASP.net 页面中加上一个,如下所示: 
       
        
       广告轮换程序 
        
        
        
       
     
        
        
       
      运行此程序,你就会得到一个旋转显示广告的页面了。 
       
      2. 日历控件 
      使用日历控件,我们可以很快生成一个日历的Web页面。代码如下: 
       
        
       广告轮换程序 
        
        
        
       
     
        
        
       
      在Visual Studio.net中,你直接从工具栏中拖过来就可以使用了。 
       
      3. 验证控件 
      以前我们我们通过自己写JavaScript脚本来验证用户输入的信息。在ASP.net中,只要我们使用验证控件这些代码也会自动生成。它能够向用户提示输入的错误信息。下面我就给大家列出这些验证控件: 
       
       
      控件名称 说明 
      RequiredFieldValidator 确保用户不跳过输入。 
      CompareValidator 使用比较运算符(小于、等于、大于等)将用户的输入与另一控件的常数值或属性值进行比较。 
      RangeValidator 检查用户的输入是否在指定的上下边界之间。 可以检查数字、字母或日期对内的范围。可以将边界表示为常数。 
      RegularExpressionValidator 检查输入是否与正则表达式定义的模式匹配。该验证类型允许检查可预知的字符序列,如社会保障号、电子邮件地址、电话号码、邮政编码等中的字符序列。 
      CustomValidator 使用您自己编写的验证逻辑检查用户的输入。该验证类型允许检查运行时导出的值。 
      ValidationSummary 以摘要的形式显示页上所有验证程序的验证错误。    
      好了,既然ASP.net有如此多的优点,让我们现在就开始安装和使用ASP.NET吧!

关键字