极品桌面
极品网文
极品日记
访客留言
加载中...
网文首页
精妙网文
爆笑网文
网页制作
小说连载
ASP
网页技术
网站相关
XML
建站经验
PHP
MS SQL
查看文章
带你走进ASP.NET(3)
作者:飞鹰 来源:www.ASPCool.com 添加时间:2003年3月6日 字体:
大
中
小
80582
1.2.2内容和代码分离
~1%
现在的网站建设通常要求开发人员做后台的程序设计,前面有专业的美工做界面设计。虽然有时候开发人员也会做些界面设计,但是通常都无法达到专业的要求。上面说过,在以前的ASP中,由于代码和HTML页面语言混杂在一起,这就使得网站的建设变得相当的困难。在ASP.NET中,微软使用代码后置很好的解决了这个问题。
~1%
我
~1%
们现在建立一个HTML页面,如下:
~1%
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
~1%
AutoEventWireup="false" Inherits="AspCool.WebForm1" %>
~1%
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
~1%
<HTML>
~1%
<HEAD>
~1%
<title>WebForm1</title>
~1%
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
~1%
<meta name="CODE_LANGUAGE" Content="C#">
~1%
<meta name="vs_defaultClientScript" content="javascript">
~1%
<meta name="vs_targetSchema" content="
http://schemas.microsoft.com/intellisense/ie5"
;>
~1%
</HEAD>
~1%
<body MS_POSITIONING="GridLayout">
~1%
<form id="Form1" method="post" runat="server">
~1%
<FONT face="宋体">
~1%
<asp:TextBox id="TextBox1" style="Z-INDEX: 101; LEFT: 47px; POSITION: absolute; TOP: 23px" runat="server"></asp:TextBox>
~1%
<asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 53px; POSITION: absolute; TOP: 76px" runat="server" Text="Button"></asp:Button></FONT>
~1%
</form>
~1%
</body>
~1%
~1%
~1%
从第一行中我
~1%
们可以看出,该页面的后台代码全都在WebForm1.aspx.cs文件中。我
~1%
们可以在这个文件中编写程序,如下所示:
~1%
using System;
~1%
using System.Collections;
~1%
using System.ComponentModel;
~1%
using System.Data;
~1%
using System.Drawing;
~1%
using System.Web;
~1%
using System.Web.SessionState;
~1%
using System.Web.UI;
~1%
using System.Web.UI.WebControls;
~1%
using System.Web.UI.HtmlControls;
~1%
~1%
namespace AspCool
~1%
{
~1%
/// <summary>
~1%
/// Summary description for WebForm1.
~1%
/// </summary>
~1%
public class WebForm1 : System.Web.UI.Page
~1%
{
~1%
protected System.Web.UI.WebControls.TextBox TextBox1;
~1%
protected System.Web.UI.WebControls.Button Button1;
~1%
~1%
private void Page_Load(object sender, System.EventArgs e)
~1%
{
~1%
// Put user code to initialize the page here
~1%
}
~1%
~1%
#region Web Form Designer generated code
~1%
override protected void OnInit(EventArgs e)
~1%
{
~1%
//
~1%
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
~1%
//
~1%
InitializeComponent();
~1%
base.OnInit(e);
~1%
}
~1%
~1%
/// <summary>
~1%
/// Required method for Designer support - do not modify
~1%
/// the contents of this method with the code editor.
~1%
/// </summary>
~1%
private void InitializeComponent()
~1%
{
~1%
this.Button1.Click += new System.EventHandler(this.Button1_Click);
~1%
this.Load += new System.EventHandler(this.Page_Load);
~1%
~1%
}
~1%
#endregion
~1%
~1%
private void Button1_Click(object sender, System.EventArgs e)
~1%
{
~1%
~1%
}
~1%
}
~1%
}
~1%
通过代码后置,开发人员可以直接修改.cs文件(在Visual Basic.NET中是.vb文件)。而页面设计人员可以修改HTML页面,这样就大大简化了网站的建设过程。
~1%
1.2.3 ASP.NET丰富的Web控件
~1%
ASP.NET的另外一个优点就是给我
~1%
们提供了大量的丰富的Web控件。你可以在System.Web.UI.WebControls名字空间下找到各种各样的Web控件,这些控件中包括运行在服务端的from控件,例如:Button、TextBox等,同时也包括一些特殊用途的控件,如:广告轮换控件、日历控件,以及用户验证控件等。下面我
~1%
们就具几个例子来看看如何使用这些控件。
~1%
~1%
1. 广告轮换控件<asp:adrotator>
~1%
广告轮换控件可以在网页上显示旋转的广告。广告链接信息保存在一个XML文件中,如ads.xml。
~1%
~1%
<?xml version="1.0" encoding="utf-8" ?>
~1%
<Advertisements>
~1%
~1%
<Ad>
~1%
<ImageUrl>
http://www.aspcool.com/images/newaspcool.gif<
;/ImageUrl>
~1%
<NavigateUrl>
http://www.aspcool.com<
;/NavigateUrl>
~1%
<AlternateText>Alt Text</AlternateText>
~1%
<Keyword>Computers</Keyword>
~1%
<Impressions>80</Impressions>
~1%
</Ad>
~1%
~1%
<Ad>
~1%
<ImageUrl>
http://www.aspcool.com/images/chat.gif<
;/ImageUrl>
~1%
<NavigateUrl>
http://www.chaxiu.com<
;/NavigateUrl>
~1%
<AlternateText>Alt Text</AlternateText>
~1%
<Keyword>Computers</Keyword>
~1%
<Impressions>80</Impressions>
~1%
</Ad>
~1%
</Advertisements>
~1%
~1%
我
~1%
们现在在asp.net 页面中加上一个<asp:adrotator>,如下所示:
~1%
<HTML>
~1%
<HEAD>
~1%
<title>广告轮换程序</title>
~1%
</HEAD>
~1%
<body MS_POSITIONING="GridLayout">
~1%
<form id="Form1" method="post" runat="server">
~1%
<asp:AdRotator id="AdRotator1" runat="server" Width="468px" Height="60px" AdvertisementFile="ads.xml"></asp:AdRotator>
~1%
</form>
~1%
</body>
~1%
</HTML>
~1%
运行此程序,你就会得到一个旋转显示广告的页面了。
~1%
~1%
2. 日历控件<asp:calendar>
~1%
使用日历控件,我
~1%
们可以很快生成一个日历的Web页面。代码如下:
~1%
<HTML>
~1%
<HEAD>
~1%
<title>广告轮换程序</title>
~1%
</HEAD>
~1%
<body MS_POSITIONING="GridLayout">
~1%
<form id="Form1" method="post" runat="server">
~1%
<asp:Calendar id="Calendar1" runat="server"></asp:Calendar>
~1%
</form>
~1%
</body>
~1%
</HTML>
~1%
在Visual Studio.NET中,你直接从工具栏中拖过来就可以使用了。
~1%
~1%
3. 验证控件
~1%
以前我
~1%
们我
~1%
们通过自己写javascript脚本来验证用户输入的信息。在ASP.NET中,只要我
~1%
们使用验证控件这些代码也会自动生成。它能够向用户提示输入的错误信息。下面我
~1%
就给大家列出这些验证控件:
~1%
~1%
~1%
控件名称 说明
~1%
RequiredFieldValidator 确保用户不跳过输入。
~1%
CompareValidator 使用比较运算符(小于、等于、大于等)将用户的输入与另一控件的常数值或属性值进行比较。
~1%
RangeValidator 检查用户的输入是否在指定的上下边界之间。 可以检查数字、字母或日期对内的范围。可以将边界表示为常数。
~1%
RegularExpressionValidator 检查输入是否与正则表达式定义的模式匹配。该验证类型允许检查可预知的字符序列,如社会保障号、电子邮件地址、电话号码、邮政编码等中的字符序列。
~1%
CustomValidator 使用您自己编写的验证逻辑检查用户的输入。该验证类型允许检查运行时导出的值。
~1%
ValidationSummary 以摘要的形式显示页上所有验证程序的验证错误。
~1%
~1%
好了,既然ASP.NET有如此多的优点,让我
~1%
们现在就开始安装和使用ASP.NET吧!
80579
返回页面顶端
上一篇:
带你走进ASP.NET(2)
下一篇:
带你走进ASP.NET(4)
返回上一页
打印本文
加入收藏
页面最后更新时间:2010年3月10日
相关文章:
带你走进ASP.NET(4)
带你走进ASP.NET(2)
带你走进ASP.NET(1)