Webform——注册验证

   服务器控件和客户端控件的交替使用,主要还是获取到各个控件的值,直接用C#或Js判断是否符合条件就可以。

   这里是以服务器控件为例子,至于客户端控件则需要写JS代码

1、UserBF
public class UserBF
{
    private MyDBDataContext _context;
    public UserBF()
    {
        _context = new MyDBDataContext();
    }
    public bool Select(string UserName,string UserPwd)  //验证用户名
    {
        var query = _context.LoginUser.Where(P=>P.UserName==UserName&&P.UserPwd==UserPwd);
        return query.Count() > 0;  
    }
    public bool Login(string UserID)//验证ID是否唯一
    {
        var query = _context.LoginUser.Where(P => P.UserID==UserID);
        return query.Count() > 0;  
    }
    public void Insert(string userid,string username,string userpwd)
    {
        LoginUser data = new LoginUser();
        data.UserID = userid;
        data.UserName = username;
        data.UserPwd = userpwd;
        _context.LoginUser.InsertOnSubmit(data);
        _context.SubmitChanges();
    }
}
2、前台设计
 <form id="form1" runat="server" >
    <div>
    <center><h1>注册页面</h1></center>
       
         <div>
        <center>
            <h1>服务器控件</h1>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<asp:Label ID="Label5" runat="server" Text="用户ID:"></asp:Label>&nbsp;<asp:TextBox ID="TextBox2" runat="server" OnTextChanged="TextBox2_TextChanged"></asp:TextBox><input id="Button3" type="button" value="唯一验证" runat="server" onserverclick="Button3_ServerClick" ><br>
        <asp:Label ID="Label6" runat="server" Text="用户名:"></asp:Label>&nbsp;<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br>
        <asp:Label ID="Label7" runat="server" Text="密码:"></asp:Label>&nbsp;&nbsp;&nbsp;<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><br>
            <asp:Label ID="Label8" runat="server" Text="确认密码:"></asp:Label><asp:TextBox ID="TextBox5" runat="server"></asp:TextBox><br>
             <asp:Button ID="Button4" runat="server" Text="注册"  Enabled="False" OnClick="Button1_Click" />
            </center>
    </div>
</from>
3、在C#后台写验证的代码
protected void Button1_Click(object sender, EventArgs e)//服务器的注册
    {
        string userid = TextBox2.Text;
        string username = TextBox3.Text;
        string userpwd = TextBox4.Text;
        string userpwdagain = TextBox5.Text;
        if(userid=="")
        {
            Response.Write("<script>alert('用户ID不能为空!!')</script>");
        }
        else if(username=="")
        {
            Response.Write("<script>alert('用户名不能为空!!')</script>");
        }
        else if (userpwd == "")
        {
            Response.Write("<script>alert('用户密码不能为空!!')</script>");
        }
        else if (userpwd != userpwdagain)
        {
            Response.Write("<script>alert('两次密码不一样,请重新输入!!')</script>");
        }
        else
        {
            new UserBF().Insert(userid,username,userpwd);
            Response.Write("<script>alert('注册成功!!')</script>");
        
        }
    }
    protected void Button3_ServerClick(object sender, EventArgs e)//服务器唯一验证
    {
        if (string.IsNullOrWhiteSpace(TextBox2.Text))
        {
            Response.Write("<script>alert('用户ID不能为空!!')</script>");
            
        }
        else
        {
            bool isok = new UserBF().Login(TextBox2.Text);
            if (isok)
            {
                Response.Write("<script>alert('此ID已存在,请更改!!')</script>");
            }
            else
            {
                Response.Write("<script>alert('恭喜你可以使用!!!')</script>");
                Button4.Enabled = true;
            }
        }
    }
    protected void TextBox2_TextChanged(object sender, EventArgs e)
    {
        Button4.Enabled = false;
    }

效果图:

→→

 

posted @ 2015-07-16 20:53  XCml  阅读(604)  评论(0编辑  收藏  举报