登录、注册页面及后台代码
来源:互联网 发布:数字网络配线架 编辑:程序博客网 时间:2024/05/19 16:06
一.登录页面及后台代码
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"); } }}
0 0
- 登录、注册页面及后台代码
- Mysqli实现的注册登录后台代码
- php登录注册页面及加载
- jquery后台登录页面背景图片自动轮换登录界面代码
- 注册页面后台代码-当需跨表插入数据
- struts登录注册页面
- 注册/登录页面
- jQueryMobile 登录、注册页面
- 一个注册登录页面
- 仿美团登录注册页面
- 注册登录页面跳转
- 后台学习五---登录注册
- 登录注册代码
- 手机号码注册登录代码
- 正则表达式及js校验表单代码(注册页面)
- Jsp页面注册与登录
- Axure设计登录注册页面
- Drupal 定制登录注册页面
- Java学习路线图·影响一代又一代程序员的经典书籍!
- java选择排序
- TJOI2015Day1测试总结
- nginx源码学习——命令行选项处理
- OC和Swift混编(惊喜!)
- 登录、注册页面及后台代码
- java基础——java中的异常
- c++中BOOL和bool的区别
- Android实现网络多线程断点续传下载
- JavaScript Notes 1
- 算法导论 15章 动态规划
- php-fpm 启动参数及重要配置详解
- C++函数重载
- <NSCoding>存储数据 archivedDataWithRootObject unarchiveObjectWithData