登录、注册页面及后台代码
2015-05-12 20:29
253 查看
一.登录页面及后台代码
1.登录页面如图1所示
首先进行身份选择,由“管理员”和“用户”两种身份进行选择,选择不同的身份,程序会进入不同的数据表检索登录信息;当用户名或密码为空时会提示;当用户名或密码在数据表中没有检索到时,会提示登录失败;
有一些细节优化,在程序中有所体现:
(1)由“管理员”和“用户”两种身份进行选择,选择不同的身份,程序会进入不同的数据表检索登录信息;
(2)管理员和用户两种身份登录,会跳转到不同的页面;//Response.Redirect("AdminPage.aspx");
(3)使用session暂存用户名信息,实现不同页面之间的信息共享,在另一个页面直接应用session即可;//Session["LoginName"] = TextBox1.Text;
图1
2.后台代码如下:
1.注册页面如图2所示:
(1)输入用户名时,连接数据库验证用户名是否已经存在,若存在会提示信息
(2)验证输入是否为空,使用RequiredFieldValidator控件;验证输入是否为邮箱格式,使用RegularExpressionValidator控件
图2
2.程序后台代码如下:
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"); } } }
相关文章推荐
- jquery后台登录页面背景图片自动轮换登录界面代码
- 使用Jquery搭建最佳用户体验的登录页面之记住密码自动登录功能(含后台代码)
- 使用Jquery搭建最佳用户体验的登录页面 之 记住密码自动登录功能,(含后台代码)
- jsp_首先利用纯HTML写一个简单的登录页面以及后台处理页面【代码】
- Mysqli实现的注册登录后台代码
- C# 从后台代码同步或异步注册Javascript到页面之RegisterStartupScript和RegisterClientScriptBlock的区别
- Appcan与后台数据库表交互,注册登录完整页面
- 注册页面后台代码-当需跨表插入数据
- 源码推荐(01.08B):一行代码搞定倒计时,登录注册找回密码页面逻辑
- 使用Jquery搭建最佳用户体验的登录页面之记住密码自动登录功能(含后台代码)
- eShop电子商城的SSH实现的用户模块(1)登录和注册配置文件和页面关键代码(V...
- springboot 整合 MongoDB 实现登录注册,html 页面获取后台参数的方法
- 漂亮的css+div会员注册或登录页面代码
- asp.net 后台代码跳转页面前弹出提示框
- jquery 弹出层注册页面等(asp.net后台)
- 登录注册功能的实现,核心的验证代码
- ASP.NET登录注册页面实现
- MOSS 2007 应用随笔(30)--为sharepoint的内部页面添加后台代码
- JSP实现登录注册并链接数据库页面
- Laravel5.4 定义中间件,没有登录访问后台跳转到指定页面