做好准備工作,現在就要動真格的了,先讓我們看看用戶注冊的實現。前邊已經講過,ASP.Net可以實現業務邏輯和Html代碼分離,那麼讓我們來看看到底是如何實現的,下面這個文件是用戶注冊的頁面部分,原型是根據我站點的風格制作的:
<%@Page language="c#" Codebehind="Register.cs" AutoEventWireup="false" Inherits="bbs.Register" %>
<%@Register Tagprefix="My" Namespace="bbs.uctrl"%>
<Html><head>
<TITLE>新用戶注冊</TITLE>
<meta name=vs_targetSchema content="Html 4.0">
<link rel="stylesheet" href="images/style.CSS">
<META http-equiv=Content-Type content="text/Html; charset=gb2312">
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#"></head>
<script language=Javascript>
function OnPrevIEw()
{
divPrevIEw.innerHtml = Form1.txtSignature.value ;
}
</script>
<body>
<form method="post" runat="server" ID=Form1>
<My:MyHead id="myHead1" runat="server"></MY:MYHEAD>
<!----------------------外面表格形成邊框-------------------------------------->
<table width='722' border='0' cellspacing=0
cellpadding='0' align='center'>
<TBODY>
<tr>
<!-------------------左邊豎線---------------------------------------------->
<td bgcolor='#0097c0' width='1'>
<img src='images/Shim.gif' width=1>
</td>
<td width=720 align=middle><br><br><br>
<!-------------------左邊豎線---------------------------------------------->
<!--------------新用戶注冊開始---------------------------------------------->
<table width=600 align=center border=0 cellpadding=4 cellspacing=1
id="tblRegister" class=cn bgcolor=#000000 runat=server>
<TBODY>
<tr bgcolor=#ffffff>
<td colspan=3>
<p align=center>新用戶注冊</p>
</td>
</tr>
<!--------------用戶名開始-------------------------------------------------------->
<tr bgcolor=#ffffff>
<td width=60>
用戶名
</td>
<td width=300>
<asp:TextBox id="txtUserName" maxlength=20 columns=20 runat="server"></ASP:TextBox>
<font color=red>*</font>
</td>
<td width=240>
用戶筆名,4-20字符
<ASP:requiredfIEldvalidator id="reqUserName" display=Dynamic
controltovalidate="txtUserName" runat=Server>
不能為空!
</ASP:requiredfIEldvalidator>
<ASP:regularexpressionvalidator id="regUserName" display=Dynamic
controltovalidate="txtUserName" runat=Server validationexpression="[^']{4,20}">
用戶名非法!
</ASP:regularexpressionvalidator>
<ASP:customvalidator id="cusUserName" controltovalidate="txtUserName"
onservervalidate="ValidUser" display=Dynamic runat=Server>
該用戶已存在。
</ASP:customvalidator>
</td>
</tr>
<!--------------用戶名結束-------------------------------------------------------->
<!--------------用戶密碼開始-------------------------------------------------------->
<tr bgcolor=#ffffff>
<td width=60>
密碼
</td>
<td width=300>
<ASP:TextBox id="txtPassWord" maxlength=10 columns=10
textmode=PassWord runat=server></ASP:TextBox>
<font color=red>*</font>
</td>
<td width=240>
用戶密碼,4-10字符
<ASP:requiredfieldvalidator id=RequiredfIEldvalidator1 display=Dynamic
controltovalidate="txtPassWord" runat=Server>
不能為空!
</ASP:requiredfIEldvalidator>
<ASP:regularexpressionvalidator id=Regularexpressionvalidator1 display=Dynamic
controltovalidate="txtPassWord" runat=Server validationexpression="[^']{4,10}">
密碼非法!
</ASP:regularexpressionvalidator>
</td>
</tr>
<!--------------用戶密碼結束-------------------------------------------------------->
<!--------------驗證密碼開始-------------------------------------------------------->
<tr bgcolor=#ffffff>
<td width=60>
驗證密碼
</td>
<td width=300>
<ASP:TextBox id=txtPassWord1 maxlength=10 columns=10
textmode=PassWord runat=server></ASP:TextBox>
<font color=red>*</font>
</td>
<td width=240>
再次輸入密碼。
<ASP:comparevalidator id="comPassWord" display=Dynamic
controltocompare="txtPassword" controltovalidate="txtPassWord1" runat=Server>
兩次錄入的密碼不同!
</ASP:comparevalidator>
</td>
</tr>
<!--------------驗證密碼結束-------------------------------------------------------->
<!--------------email開始-------------------------------------------------------->
<tr bgcolor=#ffffff>
<td width=60>
Email
</td>
<td width=300>
<asp:TextBox id="txtEmail" maxlength=100 columns=30 runat=server></ASP:TextBox>
</td>
<td width=240>
您的電子郵件地址,您可以不填,但請不要胡填。
<ASP:regularexpressionvalidator id="regEmail" display=Dynamic
controltovalidate="txtEmail" validationexpression="[^']*" runat=Server>
非法字符
</ASP:regularexpressionvalidator>
</td>
</tr>
<!--------------email結束-------------------------------------------------------->
<!--------------個人主頁開始-------------------------------------------------------->
<tr bgcolor=#ffffff>
<td width=60>
個人主頁
</td>
<td width=300>
<asp:TextBox id="txtHomepage" maxlength=150 columns=30 runat=server></ASP:TextBox>
</td>
<td width=240>
您的主頁,您可以不填,但請不要胡填。
<ASP:regularexpressionvalidator id="regHomepage" display=Dynamic
controltovalidate="txtHomepage" validationexpression="[^']*" runat=Server>
非法字符。
</ASP:regularexpressionvalidator>
</td>
</tr>
<!--------------個人主頁結束-------------------------------------------------------->
<!--------------簽名開始-------------------------------------------------------->
<tr bgcolor=#ffffff>
<td width=60>
簽名
</td>
<td width=300>
<asp:TextBox id="txtSignature" maxlength=150 columns=30 rows=6 textmode=MultiLine runat=server></ASP:TextBox>
</td>
<td width=240 valign=top>
<div id="divPrevIEw">
你可以制作自己的簽名,不超過255個字符,不能用script。<br><br>
</div><br>
<input type=button id="btnPreview" value="預覽" onclick="OnPrevIEw()"><br>
<ASP:regularexpressionvalidator id=Regularexpressionvalidator2 display=Dynamic
controltovalidate="txtSignature" validationexpression="[^']{0,255}" runat=Server>
使用非法字符或超過255個字符。
</ASP:regularexpressionvalidator>
</td>
</tr>
<!--------------簽名結束-------------------------------------------------------->
<tr bgcolor=#ffffff>
<td colspan=3 align=center>
<asp:button id="btnSubmit" text="確認" onclick="OnSubmit" runat=Server></ASP:button>
</td>
</tr>
</table><br><br><br>
<!----------------------------------新用戶注冊結束---------------------->
<!-------------------右邊豎線---------------------------------------------->
<td bgcolor='#0097c0' width='1'>
<img src='images/Shim.gif' width=1>
</td>
<!-------------------右邊豎線---------------------------------------------->
</tr>
<!-------------------下邊橫線---------------------------------------------->
<tr>
<td colspan=3 height=1 bgcolor=#0097c0><img src='images/shim.gif'width=1 height=1></td>
</tr>
<!-------------------下邊橫線---------------------------------------------->
</table>
<My:bottom id="myBottom" runat="server"></My:bottom>
</form>
</BODY></Html>
<!--------------------文件結束--------------------------------->
怎麼樣,看上去很熟悉吧,除了頁首兩句及下面webform中帶runat=server的webcontrol,是不是和普通的Html一樣?注意到頁首第一句中的Codebehind="Register.cs"嗎,它指定本頁後面的代碼文件是register.cs,這是ASP.Net提供的一種機制,它可以將業務邏輯隱藏在與.aspx同名的cs文件中,而運行時先把這個cs文件編譯,這樣不但可以提高運行效率,也使代碼隱藏起來,避免了ASP中由於系統漏洞而造成源碼洩漏所造成的問題。那麼,這個包含業務邏輯的代碼文件是怎樣的呢?下面是這個文件:
namespace bbs
{
using System;
using System.Collections;
using System.ComponentModel;
using System.Data.SQL;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using bbs.uctrl ;
//using bbs.MyClass ;
using MyOwnClass ;
/// <summary>
/// Summary description for Register.
/// </summary>
public class Register : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btnSubmit;
protected System.Web.UI.WebControls.TextBox txtHomepage;
protected System.Web.UI.WebControls.TextBox txtEmail;
protected System.Web.UI.WebControls.CompareValidator comPassWord;
protected System.Web.UI.WebControls.TextBox txtPassWord1;
protected System.Web.UI.WebControls.RegularExpressionValidator Regularexpressionvalidator1;
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredfIEldvalidator1;
protected System.Web.UI.WebControls.TextBox txtPassWord;
protected System.Web.UI.WebControls.CustomValidator cusUserName;
protected System.Web.UI.WebControls.RegularExpressionValidator regUserName;
protected System.Web.UI.WebControls.RequiredFIEldValidator reqUserName;
protected System.Web.UI.WebControls.Label lblMessage;
protected System.Web.UI.WebControls.TextBox txtUserName;
public MyHead myHead1 ;
//構造函數
public Register()
{
Page.Init += new System.EventHandler(Page_Init);
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//
// Evals true first time browser hits the page
//
}
}
protected void Page_Init(object sender, EventArgs e)
{
//
// CODEGEN: This call is required by the ASP+ Windows Form Designer.
//
InitializeComponent();
this.myHead1.Position = 2 ;
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler (this.Page_Load);
}
//監測用戶是否存在
public bool ValidUser(Object sender , string value)
{
BBSUser myUser = new BBSUser() ;
bool bExists ;
try
{
bExists = myUser.GetUser(this.txtUserName.Text) ;
}
catch(Exception e) //如果出現異常
{
#if DEBUG
Response.Write (e.Message) ;
return false ;
#endif
Server.Transfer("error.ASPx") ;
}
return !bExists ;
}
//提交按鈕點擊
public void OnSubmit(Object sender , EventArgs e)
{
if (Page.IsValid)
{
//數據入庫
try
{
BBSUser myUser = new BBSUser() ;
if(!myUser.GetUser(txtUserName.Text))
{
myUser.CreateUser(BBSUser.CreateType.Create , txtUserName.Text , txtPassWord.Text ,
txtEmail.Text , txtHomepage.Text , "") ;
}
}
catch(Exception exp)
{
#if DEBUG
Response.Write ("出現異常:" + exp.Message) ;
return ;
#endif//DEBUG
Server.Transfer("error.ASPx") ;
}
}
}
}
}
什麼?還是看著眼熟?沒錯,是不是和前邊我定義的那個類差不多?是,本來ASP.Net就是把這個頁當作一個對象,注意類定義的那行代碼:public class Register : System.Web.UI.Page , 前面你可以理解,是定義一個Register對象,那:號後面的System.Web.UI.Page是什麼意思呢?它說明這個Regsiter類是System.Web.UI.Page類的派生類(子類),也就是說Register類除了自己成員變量、屬性、方法外,還繼承System.Web.UI.Page類的所有公共(public)或保護(protected)成員變量、屬性和方法。