asp实现注册登录界面_asp.net 实现用户登录和注册——基于webform模式
![](/assets/blank.gif)
环境:Windows10 + VS2015 + 自带LocalDB
看一下效果:
1、注册页面:
如果用户重名:
2、登录页:
3、注册或者登录好了会跳到Home页面并且显示当前的用户
下面看看关键代码:
①注册前台页面Register.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Register.aspx.cs" Inherits="Album.OnlineAlbum.Register" %>
span style="box-sizing: border-box;outline: 0px;overflow-wrap: break-word;">html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>注册title>
<style>
form{
color:#575454;
width:500px;
margin:20px auto;
font-size:15px;
}
.label{
color:red;
font-size:12px;
font-family:'Lucida Console';
}
input.Tb{
border-radius:5px;
}
.user_name{ width:240px; height:38px; line-height:38px; border:1px solid #000; background:url(login_img_03.png) no-repeat left center; padding-left:30px; }
.user_name input{ width:230px; height:36px; border:1px solid #fff;color:#666;}
.password{ width:240px; height:38px; line-height:38px; border:1px solid #dfe1e8; background:url(login_img_09.png) no-repeat left center; padding-left:30px; }
.password input{ width:230px; height:36px; border:1px solid #000;color:#666;}
.transButton {
border:solid 1px;
background-color:transparent;
}
#btnRegister{
font-size:14px;
}
#linkToLogin{
text-decoration:none
}
#ckItem{
text-decoration:none
}
body{
background-image:url("rbg.jpg");
}
style>
head>
<body>
<form id="form1" runat="server">
<h2>欢迎注册OAh2>
<h3>每一天,记录美。h3>
<br />
<asp:ScriptManager ID="ScriptManager1" runat="server">
asp:ScriptManager>
<asp:TextBox runat="server" ID="rUserNameText" Height="40px" Width="490px" CssClass="Tb">asp:TextBox>
<br />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:CustomValidator ID="CustomValidator1" runat="server"
ControlToValidate="rUserNameText" ErrorMessage="*"
onservervalidate="CustomValidator1_ServerValidate">
asp:CustomValidator>
ContentTemplate>
asp:UpdatePanel>
<br />
<asp:TextBox runat="server" ID="rPsdText" TextMode="Password" Height="40px" Width="490px" CssClass="Tb">asp:TextBox>
<br />
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:CustomValidator ID="CustomValidator2" runat="server"
ControlToValidate="rPsdText" ErrorMessage="*"
onservervalidate="CustomValidator2_ServerValidate">
asp:CustomValidator>
ContentTemplate>
asp:UpdatePanel>
<br />
<asp:TextBox runat="server" ID="rrPsdText" TextMode="Password" Height="40px" Width="490px" CssClass="Tb" >asp:TextBox>
<br />
<asp:UpdatePanel ID="UpdatePanel3" runat="server">
<ContentTemplate>
<asp:CustomValidator ID="CustomValidator3" runat="server"
ControlToValidate="rrPsdText" ErrorMessage="*"
onservervalidate="CustomValidator3_ServerValidate">
asp:CustomValidator>
ContentTemplate>
asp:UpdatePanel>
<br />
<table>
<tr>
<td>
<asp:CheckBox ID="CheckBox1" runat="server" Checked="true" />
td>
<td>
<span>同意span> <asp:LinkButton runat="server" Text="服务条款" ID="ckItem">asp:LinkButton>
td>
<td>
td>
<td>
<asp:LinkButton ID="linkToLogin" runat="server" Text="已有账号?登录" OnClick="linkToLogin_Click">asp:LinkButton>
td>
tr>
table>
<asp:Button ID="btnRegister" runat="server" CssClass="transButton" Height="49px" Text="注 册" Width="500px" OnClick="btnRegister_Click" />
form>
body>
<script type="text/javascript">
function watermark(id, value) {
var obj = document.getElementById(id);
var isPsdMode = false;
if (obj.type == "password")
{
obj.type = "text";
isPsdMode = true;
}
obj.value = value;
obj.style.color = "Gray";
//获取焦点事件
obj.onfocus = function () {
obj.style.color = "Black";
if (isPsdMode)
{
obj.type = "password";
}
if (this.value == value) {
this.value = '';
}
};
//失去焦点事件
obj.onblur = function () {
if (this.value == "") {
if (isPsdMode) {
obj.type = "text";
}
this.value = value;
obj.style.color = "Gray";
}
else {
obj.style.color = "Black";
}
};
}
window.onload = function () {
var arr = [{ 'id': 'rUserNameText', 'desc': '用户名' }, { 'id': 'rPsdText', 'desc': '密码' },{ 'id': 'rrPsdText', 'desc': '确认密码' }];
for (var i = 0; i < arr.length; i++) {
watermark(arr[i].id, arr[i].desc);
}
};
script>
html>
注册后台页面Register.aspx.cs:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
namespace Album.OnlineAlbum
{
public partial class Register : System.Web.UI.Page
{
private bool UserNameIselgal = false;
private bool PsdIselgal = false;
private bool CanRegister = false;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void linkToLogin_Click(object sender, EventArgs e)
{
Response.Redirect("Login.aspx");
}
protected void btnRegister_Click(object sender, EventArgs e)
{
Session["User"] = rUserNameText.Text;
Session["Psd"] = rPsdText.Text;
string connStr = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True";
SqlConnection conn = new SqlConnection(connStr);
try
{
conn.Open();
SqlCommand cmd = new SqlCommand("select count(*) from UserTable where UId=@UId", conn);
cmd.Parameters.Add("@UId", SqlDbType.Char);
cmd.Parameters[0].Value = Session["User"];
int count = (int)cmd.ExecuteScalar();
if (count > 0)
{
Response.Write(");
}
else
{
CanRegister = true;
}
}
catch
{
Response.Write("检测重名异常");
}
finally
{
conn.Close();
}
if (CanRegister)
{
try
{
conn.Open();
string strIns = "insert into UserTable(UId, Psd) values(@UId, @Psd)";
SqlCommand cmd = new SqlCommand(strIns, conn);
cmd.Parameters.Add("@UId", SqlDbType.NChar);
cmd.Parameters.Add("@Psd", SqlDbType.NChar);
cmd.Parameters["@UId"].Value = Session["User"];
cmd.Parameters["@Psd"].Value = Session["Psd"];
cmd.ExecuteNonQuery();
}
catch
{
Response.Write("注册异常");
}
finally
{
conn.Close();
}
}
CanRegister = CanRegister && UserNameIselgal && PsdIselgal;
if (CanRegister)
{
Session["CurrentUser"] = rUserNameText.Text;
Response.Redirect("Home.aspx");
}
}
protected void CustomValidator1_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
{
if (rUserNameText.Text.Equals("用户名"))
{
CustomValidator1.ErrorMessage = "*用户名为空";
args.IsValid = false;
} else if (System.Text.RegularExpressions.Regex.IsMatch(rUserNameText.Text, "^[0-9a-zA-Z]+$") &&
rUserNameText.Text.Length > 5 && rUserNameText.Text.Length < 11)
{
args.IsValid = true;
UserNameIselgal = true;
}
else
{
CustomValidator1.ErrorMessage = "*用户名由6~10位数字和字母构成";
args.IsValid = false;
}
}
protected void CustomValidator2_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
{
if (rPsdText.Text.Equals("密码"))
{
CustomValidator2.ErrorMessage = "*密码为空";
args.IsValid = false;
}
else if (System.Text.RegularExpressions.Regex.IsMatch(rPsdText.Text, "^[0-9a-zA-Z]+$") &&
rPsdText.Text.Length > 4)
{
args.IsValid = true;
}
else
{
CustomValidator2.ErrorMessage = "*密码由全数字和字母构成且不少于5位";
args.IsValid = false;
}
}
protected void CustomValidator3_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
{
if (rrPsdText.Text.Equals("") ||rrPsdText.Text.Equals("确认密码"))
{
args.IsValid = false;
CustomValidator3.ErrorMessage = "*确认密码为空";
}
else if (!rrPsdText.Text.Equals(rPsdText.Text))
{
args.IsValid = false;
CustomValidator3.ErrorMessage = "*两次密码不一致";
}
else
{
PsdIselgal = true;
args.IsValid = true;
}
}
}
}
②登录前台页面Login.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="Album.OnlineAlbum.Login" %>
span style="box-sizing: border-box;outline: 0px;overflow-wrap: break-word;">html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>登陆title>
<style>
form{
color:#575454;
width:500px;
margin: auto;
font-size:15px;
margin-top:260px;
}
#spanpsd{
margin-left:125px;
}
#spanuser{
margin-left:110px;
}
div{
margin:30px auto;
align-content:center;
}
.textbox{
border:solid 1px;
background:rgba(0, 0, 0, 0);
}
#LinkButton1{
text-decoration:none;
color:lightblue;
margin-left:230px;
}
#Button1{
border-radius:2px;
border:solid 1px;
background-color:transparent;
margin-left:150px;
margin-top:10px;
}
body{
background-image: url("lbg.jpg");
}
style>
head>
<body>
<form id="form1" runat="server">
<div>
<div>
<span id="spanuser">用户名:span>
<asp:TextBox ID="TextBox1" runat="server" CssClass="textbox" Height="30px" Width="240px">asp:TextBox>
div>
<div>
<span id="spanpsd">密码:span>
<asp:TextBox ID="TextBox2" runat="server" CssClass="textbox" Height="30px" Width="240px" TextMode="Password">asp:TextBox>
div>
<div>
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">没有账号?注册asp:LinkButton>
<br />
<asp:Button ID="Button1" runat="server" Text="登 录" Width="270px" Height="40px" OnClick="Button1_Click" />
div>
div>
form>
body>
html>
登录后台页面Login.aspx.cs:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Album.OnlineAlbum
{
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
Response.Redirect("Register.aspx");
}
protected void Button1_Click(object sender, EventArgs e)
{
string connStr = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True";
SqlConnection conn = new SqlConnection(connStr);
try
{
conn.Open();
SqlCommand cmd = new SqlCommand("select count(*) from UserTable where UId=@UId and Psd=@Psd", conn);
cmd.Parameters.Add("@UId", SqlDbType.Char);
cmd.Parameters.Add("@Psd", SqlDbType.Char);
cmd.Parameters[0].Value = TextBox1.Text;
cmd.Parameters[1].Value = TextBox2.Text;
int count = (int)cmd.ExecuteScalar();
if (count == 1)
{
Session["CurrentUser"] = TextBox1.Text;
Response.Redirect("./Home.aspx");
}
else
{
Response.Write(");
}
}
catch
{
Response.Write(");
}
finally
{
conn.Close();
}
}
}
}
③Home.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Home.aspx.cs" Inherits="Album.OnlineAlbum.Home" %>
span style="box-sizing: border-box;outline: 0px;overflow-wrap: break-word;">html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Online Ablumtitle>
<style>
#page_header{
height:20px;
margin-left:10px;
}
style>
head>
<body>
<form runat="server">
<asp:LinkButton runat="server" OnClick="Unnamed2_Click" ID="btnToReg">注册asp:LinkButton>
<asp:LinkButton runat="server" OnClick="Unnamed1_Click" ID="btnToLog">登录asp:LinkButton>
form>
body>
html>
home.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Album.OnlineAlbum
{
public partial class Home : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["CurrentUser"] != null)
btnToReg.Text = Session["CurrentUser"].ToString();
}
protected void Unnamed1_Click(object sender, EventArgs e)
{
Response.Redirect("Login.aspx");
}
protected void Unnamed2_Click(object sender, EventArgs e)
{
Response.Redirect("Register.aspx");
}
}
}
Web.config:
<configuration>
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
connectionStrings>
<appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="none"/>
appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5.2"/>
<httpRuntime targetFramework="4.5.2"/>
<httpModules>
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"/>
httpModules>
system.web>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+"/>
compilers>
system.codedom>
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules>
<remove name="ApplicationInsightsWebTracking"/>
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"
preCondition="managedHandler"/>
modules>
system.webServer>
configuration>
最新资讯请大家关注公众号:
asp实现注册登录界面_asp.net 实现用户登录和注册——基于webform模式相关推荐
- python用户登录界面tkinter_python tkinter制作用户登录界面-Go语言中文社区
学习一下莫烦Python的tkinter教程,根据教程制作了用户登录注册页.基本功能为检查登录.注册.清明上河图观看网址http://news.sohu.com/s2015/qmsht/index.s ...
- php简单的登录界面,PHP实现简单用户登录界面
用PHP实现简单的用户登录界面,供大家参考,具体内容如下 首先要实现用户登录界面需要一个html登录表单 用户名: 密码: 重复密码: 然后开始按照流程图写PHP代码 if(trim($_POST[' ...
- python做带数据库的登录界面_Python3 Tkinkter + SQLite实现登录和注册界面
本文实例为大家分享了Python3 Tkinkter + SQLite 实现登录和注册界面,供大家参考,具体内容如下 Ubuntu14 + Python3.4 + Pycharm2018 一.设计数据 ...
- 玩转Windows个性化登录界面 (更换Windows7登录界面图片/XP仿Win7登录界面)
玩转Windows个性化登录界面 (更换Windows7登录界面图片/XP仿Win7登录界面)
- html登录界面_使用数据库制作一套注册登录系统
经过了那么多个星期的学习,终于到了使用数据库的阶段了,最基本的也就是制作注册登录与数据库连接. 首先要制作一个注册窗口 先是html界面 < 效果如图: (样子怎么样不重要,重要的是测试) 这主 ...
- 注册界面php mysql_php:用户登录注册并存入数据库的简单网页示例
学习了一下php的一些基础知识,就迫不及待的想做一些相关的实例来巩固一下自己的学习,这样才知道这些函数语法在实际上的作用和效果,下面是一个比较简单的实例,实现了用户在注册的时候,把用户的数据存到数据库 ...
- java wed登录面 代码_JavaWeb实现用户登录注册功能实例代码(基于Servlet+JSP+JavaBean模式)...
下面通过通过图文并茂的方式给大家介绍JavaWeb实现用户登录注册功能实例代码,一起看看吧. 一.Servlet+JSP+JavaBean开发模式(MVC)介绍 Servlet+JSP+JavaBea ...
- 计算机退回登录界面,win7开机怎么自动登录用户?Win进入桌面又返回登录界面故障解决...
很多人在使用WIN7电脑时,会设置开机密码,这样就会导致每次开机后需要输密码,时间长了大家会觉得太麻烦,那么win7开机怎么自动登录用户呢?下面,小编就来详细解一下Win7自动登录系统帐号的方法. w ...
- android自动登录简书,安卓实现用户登录界面
这个是我们社团的作业,本人大一,也是刚刚接触安卓,很多东西都很陌生,不懂得问我们的学长,学长觉得问题太简单了不予回答O__O "-然而很多细节困扰了我很久,去网上找方法也费了好多的冤枉时间, ...
最新文章
- 我是怎么提高单片机编程能力的?
- 海康威视 | AI算法实习生招聘(3D检测/分割/多模态融合)
- 独家|测量、建议、快速上手!你所使用的Python对象占用了多少内存?(附代码)...
- python加密库用哪个好_Python的加密库入门!
- ipv6简写还原_用什么进制表示ipv6
- MySQL字符串函数
- 微软Windows Azure Platform技术解析
- 在服务器上用Fiddler抓取HTTPS流量
- TSS描述符表的作用
- Servlet和HTTP请求协议-学习笔记01【Servlet_快速入门-生命周期方法、Servlet_3.0注解配置、IDEA与tomcat相关配置】
- 用万字长文聊一聊 Embedding 技术
- SAP Shipping address页面点了continue后的网络请求
- QT-在子控件上绘图的两种方式
- 在.Net环境下使用elasticsearch实现大数据量的搜索
- android蓝牙设置名称流程,Android 8 设置蓝牙名称 流程
- Servlet,HTTP,Ruquest,Response
- js map遍历 修改对象里面的值_前端面试之你必须要懂的原生JS
- Windows下winrm的网络类型公用的问题解决
- 无线路由器的AP、Client、WDS、WISP使用功能图解(清晰明了)
- 时间(空间)复杂度 O(N) 的理解