WebFrom----登錄註冊 鏈接數據庫

¤添加一個LINQ to SQl數據庫;html

(一) 添加一個類 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;
        }
       
    }

(二)添加一個視圖 登錄表ui

視圖源代碼: <%@ 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>

後臺代碼:spa

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>");
        }

    }
}

(三) 在添加一個驗證圖code

視圖源代碼:orm

 <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>

驗證圖後臺代碼:server

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文件;xml

(五)htm

修改AutoPostBack 改爲trueblog

相關文章
相關標籤/搜索