没有执行 onkeyup="CheckUserName(document.getElementById('UserName').value)"
login.aspx
<head id="Head1" runat="server">
<title>基于Ajax的用户验证&登录</title>
<script language="javascript" type="text/javascript" src="Check.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align: center">
<table>
<tr>
<td style="width: 100px; height: 29px;">
用户名:</td>
<td style="width: 100px; height: 29px;">
<input id="UserName" type="text" onkeyup="CheckUserName(document.getElementById('UserName').value)"/></td>
<td style="width: 100px; height: 29px;"><img id="imgflag" src="image/load.gif"></td>
</tr>
<tr>
<td style="width: 100px; height: 28px;">
密<span style="color: #ffffff">__</span>码:</td>
<td style="width: 100px; height: 28px;">
<input id="Password" type="password" /></td>
<td style="width: 100px; height: 28px;">
</td>
</tr>
<tr>
<td style="width: 100px; height: 28px;">
确认密码:</td>
<td style="width: 100px; height: 28px;">
<input id="Password1" type="password" /></td>
<td style="width: 100px; height: 28px;">
</td>
</tr>
<tr>
<td colspan="3" style="height: 26px">
<input id="btnReg" type="submit" value="注册" onclick="RegUser();" />
<input id="btnlogin" type="submit" value="登录" onclick="login()" />
<input id="Reset1" type="reset" value="重置" onclick="Set();" /></td>
</tr>
</table>
Check.js
var xmlHttp;
function createXmlHttpRequest()
{
if(window.XMLHttpRequest)
{
xmlHttp=new XMLHttpRequest();
if(xmlHttp.overrideMimeType)
{
xmlHttp.overrideMimeType("text/xml");
}
}
else if(window.ActiveXObject)
{
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
if(!xmlHttp)
{
window.alert("你的浏览器不支持创建XMLhttpRequest对象");
}
return xmlHttp;
}
//创建CheckUserName
function CheckUserName(UserName)
{
var url="DisposeEvent.aspx?Name="+UserName+"&Event=Check";
xmlHttp.open("post",url,true);
xmlHttp.onreadystatechange=CheckUserNameResult;
xmlHttp.send(null);
}
//创建用户检测的回调函数
function CheckUserNameResult()
{
if(xmlHttp.readyState==4)//服务器响应状态
{
if(xmlHttp.status==200)//代码执行状态
{
if(xmlHttp.responseText=="true")
{
document.getElementById("imgflag").src="image/true.GIF";
document.getElementById("btnReg").disabled=false;
document.getElementById("btnlogin").disabled=true;
document.getElementById("Password1").disabled=false;
}
else
{
document.getElementById("imgflag").src="image/false.GIF";
document.getElementById("btnReg").disabled=true;
document.getElementById("btnlogin").disabled=false;
document.getElementById("Password1").disabled=true;
}
}
}
}
DisposeEvent.aspx.cs
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class DisposeEvent : System.Web.UI.Page
{
CheckUser cu = new CheckUser();
AddUser au = new AddUser();
logon go = new logon();
protected void Page_Load(object sender, EventArgs e)
{
switch (Request.QueryString["Event"])
{
case "Reg":
if (au.AddUserS(Request.QueryString["Name"].ToString(), Request.QueryString["Pass"].ToString()))
{
Response.Write("true");
Response.End();
}
else
{
Response.Write("false");
Response.End();
}
break;
case "Check":
if (cu.CheckUsers(Request.QueryString["Name"].ToString()))
{
Response.Write("false");
Response.End();
}
else
{
Response.Write("true");
Response.End();
}
break;
case "login":
if (go.chklogon(Request.QueryString["Name"], Request.QueryString["Pass"]))
{
Response.Write("true");
Response.End();
}
else
{
Response.Write("false");
Response.End();
}
break;
}
}
CheckUser.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
//该源码下载自www.51aspx.com(51aspx.com)
/// <summary>
/// CheckUser 的摘要说明
/// </summary>
public class CheckUser
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbuser"].ConnectionString);
public CheckUser()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public bool CheckUsers(String name)
{
String T_name = name;
String sql = "select * from info where UserName='" + T_name + "'";
SqlCommand cmd = new SqlCommand(sql, con);
try
{
con.Open();
SqlDataReader MyReader = cmd.ExecuteReader();
if (MyReader.Read())
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
throw (ex);
}
finally
{
con.Close();
}
}
}