您的位置:首页 > 编程语言

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

2015-05-12 20:29 197 查看
一.登录页面及后台代码

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

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: