WebFrom----登陆注册 连接数据库

¤添加一个LINQ to SQl数据库;

(一) 添加一个类 MyDF

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// myDF 的摘要说明
/// </summary>
public class myDF
{
    private MyDBDataContext context = new MyDBDataContext();
    public bool Login(string UserName, string PassWord)
    {
        var query = context.Login.Where(p => p.UserName == UserName && p.Password == PassWord);
        if (query.Count()>0)
        {
            return true;
        }
        return false;
    }

    public bool yanzheng(string id)
    {
        var query = context.Login.Where(p => p.UserName == id);
        if (query.Count() > 0)
        {
            return false;
        }
        return true;

    }
    public List<zhucebiao> select()//全部查询
    {
        return context.zhucebiao.ToList();
    }
    public bool index(zhucebiao a)//插入数据库
    {
        try
        {

            context.zhucebiao.InsertOnSubmit(a);
            context.SubmitChanges();
            return true;
        }
        catch
        {
            return false;
        
        }
    }
         public bool Select1(string name)//挨个查询
        {
            var query = context.zhucebiao.Where(p=>p.name==name);
            if (query.Count()>0)
            {
                return false;
            }
            return true;
        }
       
    }

(二)添加一个视图 登陆表

视图源代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="登陆图.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    </head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
        <h1 align="center">登陆表</h1>
        <p  align="center">
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:Label ID="Label1" runat="server" ForeColor="#CC33FF" Text="用户:"></asp:Label>
            <asp:TextBox ID="TextBox1" runat="server" BackColor="#FF99CC" BorderColor="#FF9999"></asp:TextBox>
            <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="唯一验证" />
        </p>
        <p align="center" >
            <asp:Label ID="Label2" runat="server" ForeColor="#FF66FF" Text="密码:" align="center"></asp:Label>
            <asp:TextBox ID="TextBox2" runat="server" BackColor="#FF99CC"></asp:TextBox>
        </p>
        <p align="center">
            <asp:Button ID="Button1" runat="server" BackColor="#FF66FF" OnClick="Button1_Click" Text="登录" />
        </p>
    </form>
    <p style="text-align: right">
        &nbsp;</p>
</body>
</html>

后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        Login a = new Login();
        a.UserName = TextBox1.Text;
        a.Password = TextBox2.Text;
        bool isok = new myDF().Login(a.UserName, a.Password);
        if (isok == true)
        {
            Response.Write("<script> alert('登陆成功')</script>");
            base.Response.Redirect("验证图.aspx");//转到验证的界面
        }
        else
        {
            Response.Write("<script> alert('登录失败')</script>");
        }



    }


    protected void Button2_Click(object sender, EventArgs e)//唯一验证
    {
        Login a = new Login();
        a.UserName = TextBox1.Text;
        bool isok = new myDF().yanzheng(a.UserName);
        if (isok)
        {
            Response.Write("<script>alert('可以使用')</script>");
        }
        else
        {
            Response.Write("<script>alert('请换个名字')</script>");
        }

    }
}

(三) 在添加一个验证图

视图源代码:

 <div>
        
        <table align="center" >
            <tr>
            <h1 align="center">注册表</h1>
            </tr>
            <tr>
                 <td>请输入您的姓名:</td>
                 <td> 
                    <asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox5_TextChanged"></asp:TextBox>
                     <asp:Label ID="Label1" runat="server"></asp:Label>
                  </td>
             </tr>   
             <tr>
                 <td>请输入您的密码:</td>
                 <td> 
                    <asp:TextBox ID="TextBox5" runat="server" OnTextChanged="TextBox5_TextChanged" TextMode="Password"></asp:TextBox>
                  </td>
             </tr>       
            <tr>
                 <td>请确认您的密码:</td>
                 <td> 
                    <asp:TextBox ID="TextBox6" runat="server" OnTextChanged="TextBox5_TextChanged" TextMode="Password"></asp:TextBox>
                     <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="TextBox5" ControlToValidate="TextBox6" ErrorMessage="两次输入的密码不一致"></asp:CompareValidator>
                  </td>
             </tr>       
             <tr> 
                 <td>请输入您的性别:</td>
                 <td>
                     <asp:RadioButton ID="RadioButton1" runat="server"  Checked="true" GroupName="sex"/><asp:RadioButton ID="RadioButton2" runat="server"  GroupName="sex"/>&nbsp;</td>
              </tr>
              <tr>
                 <td>请输入您的手机:</td>
                  <td>
                      <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                 </td>
              </tr>
              <tr>
                 <td>请输入您的生日:</td>
                 <td> <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                 </td>
              </tr>
              <tr>
                  <td>您的户籍所在地:</td>
                  <td><asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
                  </td>
              </tr>
              <tr>
                  <td>
                    <asp:Button ID="Button1" runat="server" Text="注册" OnClick="Button1_Click" /></td>
                   <td>
                    <asp:Button ID="Button2" runat="server" Text="返回上一页" OnClick="Button2_Click" /></td>
             </tr>
        </table>
    
    </div>

验证图后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class 验证 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
   protected void TextBox5_TextChanged(object sender, EventArgs e)//验证一下数据库中是否存在,存在显示星,不存在显示X
    {
        bool isok = new myDF().Select1(TextBox1.Text);
        if (isok)
        {
            Label1.Text = "";
        }
        else
        {
            Label1.Text = "×";
        }
      

    }
    protected void Button2_Click(object sender, EventArgs e)//返回上一页
    {
        base.Response.Redirect("登陆图.aspx");//跳转页面
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        zhucebiao a = new zhucebiao();
        a.name = TextBox1.Text;
        a.pwd = TextBox5.Text;
        a.sex=RadioButton1.Checked ? " 男":"女";//三元表达式
        a.telephone = TextBox2.Text;
        a.birthday = TextBox3.Text;
        a.huji = TextBox4.Text;
        bool isok=new myDF().index(a);
        if (isok)
        {
            Response.Write("<script>alert('注册成功')</script>");
            base.Response.Redirect("登录图.aspx");
        }
        else
        {
            Response.Write("<script>alert('注册失败')</script>");
            base.Response.Redirect("验证图.aspx");
        }
       
    }
}

(四) 添加一个JS文件;

(五)

修改AutoPostBack 改成true

posted @ 2015-07-17 08:58  王文姿  阅读(781)  评论(0编辑  收藏  举报