ASP.NET 登录、注册页面及后台代码

一、登录页面及后台代码

1、登录页面

如图1所示

                                                        

                                                                                  图1

        首先进行身份选择,由“管理员”和“用户”两种身份进行选择,选择不同的身份,程序会进入不同的数据表检索登录信息;当用户名或密码为空时会提示;当用户名或密码在数据表中没有检索到时,会提示登录失败;

        有一些细节优化,在程序中有所体现:

(1)由“管理员”和“用户”两种身份进行选择,选择不同的身份,程序会进入不同的数据表检索登录信息;

(2)管理员和用户两种身份登录,会跳转到不同的页面;//Response.Redirect("AdminPage.aspx");

(3)使用session暂存用户名信息,实现不同页面之间的信息共享,在另一个页面直接应用session即可;//Session["LoginName"] = TextBox1.Text;

 

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、程序后台代码如下:

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

https://blog.csdn.net/zhangqiagn1104/article/details/45674473?utm_medium=distribute.pc_relevant_download.none-task-blog-blogcommendfrombaidu-21.nonecase&depth_1-utm_source=distribute.pc_relevant_download.none-task-blog-blogcommendfrombaidu-21.nonecas

                                                         

 

 

 

  • 25
    点赞
  • 332
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值