有了前面的知识,现在我们要进入实战。做过asp的朋友都知道表单验证是个比较头疼的问题,有经
验的web程序员有这么一句话,那就是客户端不相信服务器端,服务器端不相信客户端。什么意思呢,就
是说做表单验证时服务器端程序不能假定客户端程序是正确的而不加检测,这样如果客户端关闭
javascript就可能造成出错,而如果只做服务器端检测,那么需要提交到服务器端再返回,那么效率会大
打折扣,并且对于用户极不方便。所以只能客户端和服务器端做两次验证。现在asp.net提供了新的表单
验证机制,下面我将结合实例简单讲一下,想要了解asp.net提供的几个验证webcontrol的详细资料,可
以参照我的asp+初级教程。
在讲表单验证以前,先做点准备工作。前面谈到用asp.net开发需要转换编程思维,也就是用面向对
象的思想去考虑问题,bbs对象我们已经构造好了,现在让我们来看一下一个论坛系统中另外一个很重要
的对象:用户。可以说,论坛系统的主体是用户,没有用户那也就谈不上什么论坛了,所以围绕用户的操
作很多,比如说添加/删除用户,查询/修改用户资料等等,有些论坛还有积分机制,根据用户登录次数或
发言多少来决定积分,已表明该用户的活跃程度。那么,我们应该如何来构造论坛用户这个对象呢?看看
下面的类定义:
namespace myownclass
{
using system;
using myownclass ;
using system.data.sql ;
using system.web.util ;
////////////////////////////////////////////////////////////////////
//
// class name : bbsuser
//
// description: 论坛用户类,构造一个论坛用户对象
//
// date: 2000/02/03
//
/// ////////////////////////////////////////////////////////////////
public class bbsuser
{
//新建枚举类型,创建用户方式,创建还是修改
public enum createtype
{
create = 0 ,
modify
}
//私有成员变量
private int m_intid ; //用户id
private string m_strusername ; //用户名
private string m_strpassword ; //密码
private string m_stremail ; //用户email
private string m_strhomepage ; //个人主页
private string m_strsignature ; //签名
//属性,全部只读
public int id
{
get
{
return m_intid ;
}
}
public string username
{
get
{
return m_strusername ;
}
}
public string password
{
get
{
return m_strpassword ;
}
}
public string email
{
get
{
return m_stremail ;
}
}
public string homepage
{
get
{
return m_strhomepage ;
}
}
public string signature
{
get
{
return m_strsignature ;
}
}
//构造函数
public bbsuser()
{
//
// todo: add constructor logic here
//
m_strusername = "" ;
m_strpassword = "" ;
m_stremail = "" ;
m_strhomepage = "" ;
m_strsignature = "" ;
}
//根据用户名查询用户资料
public bool getuser(string a_strusername)
{
//如果用户名中包含单引号则抛出一个异常
if (a_strusername.indexof("'") != -1)
{
throw(new exception("用户名包含非法字符")) ;
}
bool bexists = false ;
myconnection myconn = new myconnection() ;
try
{
myconn.open() ;
sqlcommand mycommand = new sqlcommand() ;
mycommand.activeconnection = myconn ;
mycommand.commandtext = "select * from bbsuser where
username='" + a_strusername + "'";
sqldatareader myreader ;
mycommand.execute(out myreader) ;
if (myreader.read())
{
m_intid = (int)myreader["id"] ;
m_strusername = myreader["username"].tostring() ;
m_strpassword = myreader["password"].tostring() ;
m_stremail = myreader["email"].tostring() ;
m_strhomepage = myreader["homepage"].tostring() ;
m_strsignature = myreader["signature"].tostring() ;
bexists = true ;
}
else
{
bexists = false ;
}
myreader.close() ;
myconn.close() ;
}
catch(sqlexception e) //如果出现异常
{
throw(new exception("数据库异常:" + e.message)) ;
}
//返回结果
return bexists ;
}
//重载,根据用户id查找用户
public bool getuser(int a_intuserid)
{
bool bexists = false ;
myconnection myconn = new myconnection() ;
try
{
myconn.open() ;
sqlcommand mycommand = new sqlcommand() ;
mycommand.activeconnection = myconn ;
mycommand.commandtext = "select * from bbsuser where id=" +
a_intuserid.tostring() ;
sqldatareader myreader ;
mycommand.execute(out myreader) ;
if (myreader.read())
{
m_intid = (int)myreader["id"] ;
m_strusername = myreader["username"].tostring() ;
m_strpassword = myreader["password"].tostring() ;
m_stremail = myreader["email"].tostring() ;
m_strhomepage = myreader["homepage"].tostring() ;
m_strsignature = myreader["signature"].tostring() ;
bexists = true ;
}
else
{
bexists = false ;
}
myreader.close() ;
myconn.close() ;
}
catch(sqlexception e) //如果出现异常
{
throw(new exception("数据库异常:" + e.message)) ;
}
//返回结果
return bexists ;
}
//新建用户
public void createuser(bbsuser.createtype a_enumcreatetype ,string
a_strusername ,
string a_strpassword ,
string a_stremail ,
string a_strhomepage ,
string a_strsignature)
{
//监测参数有效性
if (a_strusername.indexof("'") != -1 a_strpassword.indexof("'")
!= -1
a_stremail.indexof("'") != -1
a_strhomepage.indexof("'") != -1
a_strsignature.indexof("'") != -1)
{
throw(new exception("包含非法字符")) ;
}
try
{
myownclass.myconnection myconn = new myconnection() ;
sqlcommand mycmd = new sqlcommand() ;
//判断是新建用户还是修改用户资料
if (a_enumcreatetype == bbsuser.createtype.create)
{
mycmd.commandtext = "insert into bbsuser(username ,
password , email , homepage , signature)"
+ "values('" + a_strusername + "','"
+ a_strpassword + "','"
+ a_stremail + "','" + a_strhomepage
+ "','" + a_strsignature + "')" ;
}
else
{
mycmd.commandtext = "update bbsuser set email='" +
a_stremail
+ "' , homepage='" + a_strhomepage + "' ,
signature='"
+ a_strsignature + "' where username='" +
a_strusername + "'";
}
myconn.open() ;
mycmd.activeconnection = myconn ;
mycmd.executenonquery() ;
myconn.close() ;
}
catch(sqlexception exp)
{
throw(new exception("数据库出错:" + exp.message)) ;
}
}
//取回密码
public void getpassword(string a_strusername , string a_stremail)
{
if (getuser(a_strusername) && m_stremail == a_stremail)
{
//发送email
system.web.util.mailmessage mymail = new mailmessage() ;
mymail.from = "lyp@server1.domain" ;
mymail.subject = "取回您的密码" ;
mymail.body = "请牢记您的密码:" + m_strpassword ;
mymail.to = a_stremail ;
smtpmail.send(mymail) ;
}
else
{
throw (new exception("该用户不存在")) ;
}
}
}
}
通过前面的学习,你可能已经能够看懂这个类定义的大部分内容,那些是成员变量,那些是属性,那
些是方法都可以理解了,在这里需要解释的只有以下两部分内容,首先看这段代码:
public enum createtype
{
create = 0 ,
modify
}
这段代码的作用是创建bbsuser类的一个枚举变量,写过c程序的朋友很容易理解,建立这个枚举变量
的作用是简化记忆,用容易记忆的名称代替值,比如上边这个定义,当在方法createuser里作为第一个参
数时,bbsuser.createtype.create实际的值是0,代表这个方法的目的是创建用户,而如果是
bbsuser.createtype.modify,则代表目的是修改用户资料。显而易见,用if (a_enumcreatetype ==
bbsuser.createtype.create)这样的语句比用if (a_intcreatetype == 1)更容易记忆,最大限度减少出
错的可能。
另外一个要解释的内容是:你可能已经注意到在类的定义中有两个 getuser方法的定义,其作用域和
返回值都相同,只是参数类型不同。没错,这种做法叫重载(override),是并且只能是面向对象程序语言
实现多态性的基本方法,那么这样做有什么好处呢?就是根据参数不同由类自己决定应该调用那个正确的
方法,这样讲可能有些抽象,那么举个例子来说吧,这个getuser方法的作用是取得用户资料,那么它可
以通过用户名来取得资料,也可以通过用户id来取得,如果不用函数的重载,那么我们需要建两个函数,
可能一个叫getuserfromname(string a_strname) , 而另一个是getuserformid(int a_intid),在调用时
需要判断一下决定调用那个方法,象这样:
if ( bbsuser.id != "")
{
getuserfromid(bbsuser.id) ;
}
else if(bbsuser.name != "")
{
getuserfromname(bbsuser.name) ;
}
以上两种方法孰优孰劣恐怕已经不用我说了吧。
好了,既然我们已经创建好bbsuser对象,下面就可以利用它来进行对用户的操作了。
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 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器