选择显示字体大小

asp.net高级教程(五)-实战篇(中)

做好准备工作,现在就要动真格的了,先让我们看看用户注册的实现。前边已经讲过,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)成员变量、属性和方法。明白了这些,你可以安心坐下来研究代码了,做好下面关于表单验证内容的学习准备。

  


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons