您的位置:首页技术开发ASP教程 → asp.net高级教程(五)-实战篇中

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

时间:2004/11/7 4:13:00来源:本站整理作者:蓝点我要评论(0)

asp.net高级教程(五)---实战篇(结合论坛用户操作谈asp.net表单验证)(中)



   做好准备工作,现在就要动真格的了,先让我们看看用户注册的实现。前边已经讲过,asp.net可以实现业务逻辑和html代码分离,那么让我们来看看到底是如何实现的,下面这个文件是用户注册的页面部分,原型是根据我站点的风格制作的:



<%@Page language="c#" Codebehind="Register.cs" AutoEventWireup="false" Inherits="bbs.Register" %>

<%@Register Tagprefix="My" Namespace="bbs.uctrl"%>



新用户注册







    

    



  

    

    
&nbsp;



    






   cellpadding='0' align='center'>

  

   

   

     

      

   

    

    

    

    

         

    

    



       

      





   







    

    
        id="tblRegister" class=cn bgcolor=#000000 runat=server>

        

        

            

        

        

        

            

            

            

        

        



        

        

            

            

            

        

        

        

        

        

            

            

            

        

        



        

        

            

            

            

        

        



        

        

            

            

            

        

        



        

        

            

            

            

        

        

        

        

            

        

    


                

新用户注册



            


                用户名

            


                

                *

            


                用户笔名,4-20字符

                
                    controltovalidate="txtUserName" runat=Server>

                    不能为空!

                


                
                    controltovalidate="txtUserName" runat=Server validationexpression="[^']{4,20}">

                    用户名非法!

                
                        

                
                    onservervalidate="ValidUser" display=Dynamic runat=Server>

                    该用户已存在。

                
                

            


                密码

            


                
                    textmode=Password runat=server>


                *

            


                用户密码,4-10字符

                
                    controltovalidate="txtPassword" runat=Server>

                    不能为空!

                


                
                    controltovalidate="txtPassword" runat=Server validationexpression="[^']{4,10}">

                    密码非法!

                
                                        

            


                验证密码

            


                
                     textmode=Password runat=server>


                *

            


                再次输入密码。

                
                 controltocompare="txtPassword" controltovalidate="txtPassword1" runat=Server>

                    两次录入的密码不同!

                


            


                Email

            


                

            


                您的电子邮件地址,您可以不填,但请不要胡填。

                
                    controltovalidate="txtEmail" validationexpression="[^']*" runat=Server>

                    非法字符

                    


            


                个人主页

            


                

            


                您的主页,您可以不填,但请不要胡填。

                
                    controltovalidate="txtHomepage" validationexpression="[^']*" runat=Server>

                非法字符。

                


            


                签名

            


                

            


                


                你可以制作自己的签名,不超过255个字符,不能用script。



                



                

                


                
                    controltovalidate="txtSignature" validationexpression="[^']{0,255}" runat=Server>

                使用非法字符或超过255个字符。

                


            


                

            









   

     


       

      




    

    


  







    怎么样,看上去很熟悉吧,除了页首两句及下面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 description for Register.

    ///


    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 ;

        }



        ///

        ///    Required method for Designer support - do not modify

        ///    the contents of this method with the code editor.

        ///


        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)成员变量、属性和方法。明白了这些,你可以安心坐下来研究代码了,做好下面关于表单验证内容的学习准备。






相关阅读 Mac访问Windows共享文件夹Windows 7正版系统验证方法windows 8.1系统版本号查看方法Windows 8.1系统电话激活时无法输入微软返回代码解决方法Windows 8如何调整屏幕分辨率windows8.1磁盘占用100%解决方法Mac双系统如何删除Boot Camp安装的Windows分区Apple教你如何在Mac 上运行 Windows

文章评论
发表评论

热门文章 没有查询到任何记录。

最新文章 《龙珠:超宇宙》 战斗E3 2014:瘆人僵尸《消 asp代码实现access数据导出到excel文件如何使用FSO读取Js文件内容并可以编辑修改对初学者有用的一些asp函数集学习ASP编程必会的代码

人气排行 asp代码实现access数据导出到excel文件asp不需要任何配置的伪静态实现如何使用FSO读取Js文件内容并可以编辑修改asp去除html标记和空格的代码Asp全选删除代码教大家网页伪静态知识及其2种实现方法Microsoft SQL Server 7.0安装问题(一)ASP.NET中的Code Behind技术4