1. 程式人生 > >登入、註冊頁面及後臺程式碼

登入、註冊頁面及後臺程式碼

一.登入頁面及後臺程式碼

1.登入頁面如圖1所示

首先進行身份選擇,由“管理員”和“使用者”兩種身份進行選擇,選擇不同的身份,程式會進入不同的資料表檢索登入資訊;當用戶名或密碼為空時會提示;當用戶名或密碼在資料表中沒有檢索到時,會提示登入失敗;

有一些細節優化,在程序中有所體現:

(1)由“管理員”和“使用者”兩種身份進行選擇,選擇不同的身份,程式會進入不同的資料表檢索登入資訊;

(2)管理員和使用者兩種身份登入,會跳轉到不同的頁面;//Response.Redirect("AdminPage.aspx");

(3)使用session暫存使用者名稱資訊,實現不同頁面之間的資訊共享,在另一個頁面直接應用session即可;//Session["LoginName"] = TextBox1.Text;



圖1

2.後臺程式碼如下:

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

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

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //連線資料庫
            if (DropDownList1.Text == "管理員")
            {

                SqlConnection con = new SqlConnection("Data Source=PC-PC;Initial Catalog=TunnelMonitor;Integrated Security=True");
                con.Open();
                //定義字串sql,其含義為從資料表中查詢列LoginName中TextBox1.Text的記錄,列Password中TextBox2.Text的記錄
                string sql = "select * from AdminInfo where LoginName=  '" + TextBox1.Text + "'   and Password=   '" + TextBox2.Text + "'  ";
                //定義資料介面卡da,將da的資料填充至Dataset類的物件dt中
                DataTable dt = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(sql, con);
                da.Fill(dt);
                //如果記錄為TextBox1.Text和TextBox2.Text的行已經存在
                if (dt.Rows.Count > 0)
                {
                    //將TextBox1.Text的資訊暫存
                    Session["LoginName"] = TextBox1.Text;
                    string count = Session["LoginName"].ToString();
                    //如果以管理員賬號進,就轉到AdminPage.aspx     
                    Response.Redirect("AdminPage.aspx");
                }
                //使用者輸入的內容不存在於資料庫中
                else
                {
                    Label1.Text = "您輸入的使用者名稱或密碼錯誤,登入失敗!";
                }
                con.Close();
            }

            if (DropDownList1.Text == "使用者")
            {
                SqlConnection con = new SqlConnection("Data Source=PC-PC;Initial Catalog=TunnelMonitor;Integrated Security=True");
                con.Open();
                //定義字串sql,其含義為從資料表中查詢列LoginName中TextBox1.Text的記錄,列Password中TextBox2.Text的記錄
                string sql = "select * from UserInfo where UserName=  '" + TextBox1.Text + "'   and Password=   '" + TextBox2.Text + "'  ";
                //定義資料介面卡da,將da的資料填充至Dataset類的物件dt中
                DataTable dt = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(sql, con);
                da.Fill(dt);
                //如果記錄為TextBox1.Text和TextBox2.Text的行已經存在
                if (dt.Rows.Count > 0)
                {
                    //將TextBox1.Text的資訊暫存
                    Session["UserName"] = TextBox1.Text;
                    string count = Session["UserName"].ToString();
                    //如果不是管理員賬號,即轉入
                    Response.Redirect("UserPage.aspx");

                }
                //使用者輸入的內容不存在於資料庫中
                else
                {
                    Label1.Text = "您輸入的使用者名稱或密碼錯誤,登入失敗!";
                }
                con.Close();
            }
            
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            TextBox1.Text = "";
            TextBox2.Text = "";
            Label1.Text = "";
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            Response.Redirect("Register.aspx");
        }
    }
}
二.登入頁面及後臺程式碼

1.註冊頁面如圖2所示:

(1)輸入使用者名稱時,連線資料庫驗證使用者名稱是否已經存在,若存在會提示資訊

(2)驗證輸入是否為空,使用RequiredFieldValidator控制元件;驗證輸入是否為郵箱格式,使用RegularExpressionValidator控制元件


圖2

2.程式後臺程式碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;

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

        }
          //驗證使用者名稱是否存在
        private bool CheckUser()
        {
            //連線資料庫
            SqlConnection con = new SqlConnection("Data Source=PC-PC;Initial Catalog=TunnelMonitor;Integrated Security=True");
            con.Open();
            //定義字串sql,其含義為從資料表中查詢列UserName中TextBox1.Text的記錄
            string sql = "select * from UserInfo where UserName=  '" + TextBox11.Text + "'   ";
            //定義資料介面卡da,將da的資料填充至Dataset類的物件dt中
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            da.Fill(dt);
            //如果記錄為TextBox1.Text的行已經存在
            if (dt.Rows.Count > 0)
            {
                Label11.Text = " 該使用者名稱已存在";
                return false;
            }
            else
            {
                return true;
            }
            con.Close();
        }
        //註冊按鈕
        protected void Button34_Click(object sender, EventArgs e)
        {
            //檢查使用者名稱是否已經存在
            if (CheckUser() == false)
            {
                return;
            }
            //使用者名稱未註冊
            SqlConnection con = new SqlConnection("Data Source=PC-PC;Initial Catalog=TunnelMonitor;Integrated Security=True");
            con.Open();
            SqlCommand cmd = new SqlCommand("", con);
            //在資料表中插入使用者輸入的註冊資訊
            cmd.CommandText = "INSERT INTO UserInfo(UserName,Password,Email)VALUES('" + TextBox11.Text.Trim() + "','" + TextBox22.Text.Trim() + "','" + TextBox4.Text.Trim() + "')";
            cmd.ExecuteNonQuery();
            con.Close();
            //MessageBox.Show("註冊成功,請點選“確定”返回登入介面!", "溫馨提示", MessageBoxButtons.YesNo);
            Response.Redirect("Login.aspx");
        }
        //重置按鈕
        protected void Button35_Click(object sender, EventArgs e)
        {
            TextBox11.Text = "";
            TextBox22.Text = "";
            TextBox3.Text = "";
            TextBox4.Text = "";
        }
        //返回按鈕
        protected void Button36_Click(object sender, EventArgs e)
        {
            Response.Redirect("Login.aspx");
        }

    
       
    }
}