asp登陆注册功能实现
1.asp页⾯,login.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="login.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="/1999/xhtml">
<head id="Head1" runat="server">
<title>login</title>
<script src="js/aj.js" type="text/javascript"></script>
<script src="js/loginCookies.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
if (GetCookie('username') != null && GetCookie('userpass') != null)//设置记住密码的登录页⾯
{
}
}
String.prototype.Trim = function () //⾃定义的去除字符串两边空格的⽅法
{
place(/(^\s*)|(\s*$)/g, "");
}
function login()//登录
{
var aj = new ajax();
var username = ElementById("username").value.Trim();
var userpass = ElementById("userpass").value;
var url = "login.aspx?username=" + escape(username) + "&userpass=" + escape(userpass);
<(url, callback);
function callback(obj) {
var response = sponsetext;
var res = response.split('\n');
if (res[0] == "ok") {
if (ElementById("cbRememberPwd").checked) {
SetCookie('username', username, 1); //保存密码⼀个⽉
SetCookie('userpass', userpass, 1);
}
else {
SetCookie('username', username);
SetCookie('userpass', userpass);
}
window.open("loginIndex.htm", "_self");
}
else {
alert("密码错误");
}
}
}
function reset()//重置
{
}
function enterLogin() {
if (event.keyCode == 13) //如果按下的是Enter键的话,就执⾏登录语句
{
login();
}
}
</script>
</head>
<body>
<div >
<form id="form1" runat="server">
<table border="1" cellpadding="1">
<tr>
<td align="center" valign="middle">
⽤户名:
</td>
<td align="center" valign="middle">
 <asp:TextBox ID="username" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align="center" valign="middle">
密码:
</td>
javascript登录注册界面<td align="center" valign="middle">
 <asp:TextBox ID="userpass" TextMode="Password" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align="center" colspan="3" valign="middle">
<input id="cbRememberPwd" type="checkbox" />记住密码⼀个⽉
</td>
</tr>
<tr>
<td align="center" colspan="3" valign="middle">
<%-- <input id="btnOK" type="submit" value="登录" onclick="login()" /> --%>
<asp:Button ID="btnOK" runat="server" Text="登陆" BorderStyle="None" OnClick="Button1_Click"
OnClientClick=" return login();" />
 
<input id="btnReset" type="button" value="重置" onclick="reset()" runat="server" />
<%-- <asp:Button ID="Button1"  Text="重置"  runat="server"/> --%>
</td>
</tr>
</table>
<a href="regedit.aspx">注册</a>
</form>
</div>
</body>
</html>
2.aj.js 部分的js代码
function ajax() {
function getXHR()//获取xmlhttprequest
{
var request = false;
try {
request = new XMLHttpRequest();
}
catch (trymicrosoft) {
try {
request = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (othermicrosoft) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (failed) {
request = false;
}
}
}
return request;
}
< = function (openUrl, successFun)//ajax对象的get⽅法,通过get⽅式发送请求,openUrl为请求的页⾯,successFun为成功回调执⾏的函数
{
var request = getXHR();
request.open("get", openUrl, true);
// adystatechange = function ()
// {
// if (adystate==4)
// {
// if (request.status==200)
// {
// successFun(request);
// }
// }
// };
function update() {
if (adystate == 4) {
if (request.status == 200) {
successFun(request);
}
}
}
request.send(null);
}
this.post = function (openUrl, sendContent, successFun)//ajax对象的post⽅法,通过post⽅式发送请求,openUrl为请求的页⾯,successFun为成功回调执⾏的函数    {
var request = getXHR();
request.open("post", openUrl, true);
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); //告诉服务器发送的是⽂本
function update() {
if (adystate == 4) {
if (request.status == 200) {
successFun(request);
}
}
}
request.send(sendContent);
}
}
3.loginCookies.js 代码登陆页⾯信息缓存
function SetCookie(name, value, expires, path, domain, secure) {
var today = new Date();
today.Time());
if (expires) { expires *= 2592000; }
var expires_date = new Time() + (expires));
+ (path ? ";path=" + path : "")
+ (domain ? ";domain=" + domain : "")
+ (secure ? ";secure" : "");
}
function GetCookie(name) {
var cookies = kie.split(';');
var cookie = '';
for (var i = 0; i < cookies.length; i++) {
cookie = cookies[i].split('=');
if (cookie[0].replace(/^\s+|\s+$/g, '') == name) {
return (cookie.length <= 1) ? "" : unescape(cookie[1].replace(/^\s+|\s+$/g, ''));
}
}
return null;
}
function Delcookie(name, path, domain) {
+ (domain ? ";domain=" + domain : "")
+ ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
4.login的后台台⾯,即login.aspx.cs
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;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//数据库连接字符串
string conn = "server=(local);database=TestDB;User ID=sa;Password=123456;";
SqlConnection sqlcon = new SqlConnection(conn);
if (Request["username"] != null && Request["userpass"] != null)
{
string name = Request["username"].ToString();
string pwd = Request["userpass"].ToString();
string strSql = "select * from zxl_login_users where username='" + name + "'" + " and userpass='" + pwd + "'";            sqlcon.Open();
SqlCommand Comd = new SqlCommand(strSql, sqlcon);
SqlDataReader dr = Comd.ExecuteReader();
if (dr.Read())
{
Response.Redirect("html/success.htm");
}
else
{
Response.Redirect("html/error.htm");
}
}
}
}
5.VerifyUserNameHandler.ashx
<%@ WebHandler Language="C#" Class="VerifyUserNameHandler" %>
using System;
using System.Web;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;
public class VerifyUserNameHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string _name = context.Request.QueryString["para"];
_name = string.IsNullOrEmpty(_name) ? "" : _name;
string connstring = System.Configuration.ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
System.Data.DataTable zxl_login_users = new System.Data.DataTable();
using (SqlConnection conn = new SqlConnection(connstring))
{
if (conn.State != System.Data.ConnectionState.Open)
{
conn.Open();
}
SqlCommand cmd = new SqlCommand("P_test", conn);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.Add("@username", System.Data.SqlDbType.VarChar).Value = _name;
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.SelectCommand = cmd;
adapter.Fill(zxl_login_users);
}
string[] Names = new string[zxl_login_users.Rows.Count];        //创建⼀个与表⾏数相等的固定数组
for (int i = 0; i < zxl_login_users.Rows.Count; i++)
{
Names[i] = zxl_login_users.Rows[i][0].ToString();
}
/
/string[] Names = [zxl_login_users.Copy]fn;//这⾥⽤Names数组来代替数据库中的结果集
if (Array.IndexOf<string>(Names, _name) == -1)
{
context.Response.Write("恭喜,⽤户名可以使⽤。");
}
else
{
context.Response.Write("抱歉,⽤户名已被使⽤。");
}
}
//接⼝定义的类
public bool IsReusable
{
get
{
return false;
}
}
}
6.注册页⾯
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="regedit.aspx.cs" Inherits="regedit" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
function testPass() {
var p1 = ElementById("userpass").value;
var p2 = ElementById("userpass2").value;
if (p1 != p2) {
alert("两次输⼊密码不相同");
return false;
} else {
alert("两次输⼊密码相同");
}
}
</script>
<script type="text/javascript">
var xmlHttp = null;
function createXMLHttpRequest() {
if (xmlHttp == null) {
if (window.XMLHttpRequest) {
//Mozilla 浏览器
xmlHttp = new XMLHttpRequest();
} else if (window.ActiveXObject) {
// IE浏览器
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
alert('创建失败');
}
}
}
}
}
function openAjax() {
if (xmlHttp == null) {
createXMLHttpRequest();
if (xmlHttp == null) {
alert('出错');
return;
}
}
var val = ElementById('username').value;
if (val == null || val == "") {
return false;
}
xmlHttp.open("get", "VerifyUserNameHandler.ashx?para=" + val + "&date=" + new Date(), true);
xmlHttp.send(null);
}
function xmlHttpChange() {
if (adyState == 4) {
if (xmlHttp.status == 200) {
var res = sponseText;
if (res == '恭喜,⽤户名可以使⽤。') {
setTimeout("ElementById('user').innerHTML='';", 2000);
}
else if (res == '抱歉,⽤户名已被使⽤。') {
}
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<h3>
注册页⾯</h3>
⽤户名:
<asp:TextBox ID="username" runat="server" onblur="return openAjax()"></asp:TextBox>
<span id="user"></span>
<br />
密码:<asp:TextBox ID="userpass" runat="server" TextMode="Password"></asp:TextBox>
<br />
确定密码:<asp:TextBox ID="userpass2" runat="server" TextMode="Password" onblur="return testPass()"></asp:TextBox><span            id="hid"></span>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。