文件列表:
1.Register.aspx
2.CheckUsername.aspx
3.CheckNickname.aspx
在Register.aspx中增加以下行:

Javascript主调方法
<script type="text/javascript" src="../JS/prototype.js" language="javascript"></script>
<script type="text/javascript" src="../JS/ClassBackObject.js" language="javascript"></script>
<script language="javascript" type="text/javascript">
function createRequest(type)

{
var name,cbo,urlText;

if(type=="username")
{
name = escape(document.getElementById("Username").value);
urlText = "../Ajax/CheckUsername.aspx?username=" + name;
}
else

{
name = escape(document.getElementById("Nickname").value);
urlText = "../Ajax/CheckNickname.aspx?nickname=" + name;
}
cbo = new CallBackObject();
cbo.OnComplete = Cbo_Complete;
cbo.OnError = Cbo_Error;
cbo.DoCallBack(urlText);
}
function Cbo_Complete(responseText, responseXML)

{
// 取得XML文档的根
var root = responseXML.documentElement;
var strOK
try

{
// 取得<info>结果
var info = root.getElementsByTagName('value');
var type = root.getElementsByTagName('type');
// 显示返回结果

if(info[0].firstChild.data == "0")
{
strOK = "可以注册"
}
else

{
strOK = "不能注册"
}

if(type[0].firstChild.data == "username")
{
$("userNameDIV").innerHTML = strOK;
}
else

{
$("nickNameDIV").innerHTML = strOK;
}

}catch(exception)

{

}
}
function Cbo_Error(status, statusText, responseText)

{
//alert(responseText);
}
</script>
CheckUsername.aspx代码:

Checkusername.aspx代码
using System;
using System.Data;
using System.Configuration;
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;
using System.Data.SqlClient;

public partial class Ajax_CheckUsername : System.Web.UI.Page


{
protected void Page_Load(object sender, EventArgs e)

{
//此处放置用户代码以初始化页面
string strRes;
string sqlcmd;
string strName;
int ok;
strName = HttpContext.Current.Request.QueryString["username"];
sqlcmd = "select count(*) from UserInfo where Username=@Username";

SqlParameter[] param =
{
new SqlParameter("@Username",strName),
};
ok = Convert.ToInt32(userSql.db.ExecuteScalar(sqlcmd, param));

if (ok > 0)

{

strRes = ok.ToString();
}
else

{
strRes = ok.ToString();
}

strRes = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><re><type>username</type><value>" + strRes + "</value></re>";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = "text/xml";
HttpContext.Current.Response.Write(strRes);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
}

CheckNickname.aspx代码:

Checknickname.aspx代码
using System;
using System.Data;
using System.Configuration;
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;
using System.Data.SqlClient;

public partial class Ajax_CheckUsername : System.Web.UI.Page


{
protected void Page_Load(object sender, EventArgs e)

{
//此处放置用户代码以初始化页面
string strRes;
string sqlcmd;
string strName;
int ok;
strName = HttpContext.Current.Request.QueryString["username"];
sqlcmd = "select count(*) from UserInfo where Username=@Username";

SqlParameter[] param =
{
new SqlParameter("@Username",strName),
};
ok = Convert.ToInt32(userSql.db.ExecuteScalar(sqlcmd, param));

if (ok > 0)

{

strRes = ok.ToString();
}
else

{
strRes = ok.ToString();
}

strRes = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><re><type>username</type><value>" + strRes + "</value></re>";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = "text/xml";
HttpContext.Current.Response.Write(strRes);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
}

ClassBackObject.js

ClassBackObject.js代码
// JScript 文件
function CallBackObject()


{
this.XmlHttp = this.GetHttpObject();
}

CallBackObject.prototype.GetHttpObject = function()


{
var xmlhttp;

/**//*@cc_on
@if (@_jscript_version >= 5)
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP")
}
catch (e){
try{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (E){
xmlhttp = false;
}
}
@else
xmlhttp = false;
@end @*/

if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
{

try
{
xmlhttp = new XMLHttpRequest();

} catch (e)
{
xmlhttp = false;
}
}
return xmlhttp;
}

CallBackObject.prototype.DoCallBack = function(URL)


{

if (this.XmlHttp)
{

if(this.XmlHttp.readyState == 4 || this.XmlHttp.readyState == 0)
{
var oThis = this;
this.XmlHttp.open('POST',URL);

this.XmlHttp.onreadystatechange = function()
{oThis.ReadyStateChange();};
//this.XmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
this.XmlHttp.send(null);
}
}
}

CallBackObject.prototype.AbortCallBack= function()


{
if(this.XmlHttp)
this.XmlHttp.abort();
}

CallBackObject.prototype.OnLoading = function()


{
// Loading
}

CallBackObject.prototype.OnLoaded = function()


{
// Loaded
}

CallBackObject.prototype.OnInteractive = function()


{
// Interactive
}

CallBackObject.prototype.OnComplete = function()


{
// Complete
}

CallBackObject.prototype.OnAbort = function()


{
// Abort
}

CallBackObject.prototype.OnError = function()


{
// Error
}

CallBackObject.prototype.ReadyStateChange = function()


{
if (this.XmlHttp.readyState == 1)

{
this.OnLoading();
}
else if(this.XmlHttp.readyState == 2)

{
this.OnLoaded();
}
else if(this.XmlHttp.readyState == 3)

{
this.OnInteractive();
}
else if(this.XmlHttp.readyState == 4)

{
if(this.XmlHttp.status == 0)
this.OnAort();
else if(this.XmlHttp.status == 200 && this.XmlHttp.statusText == "OK")
this.OnComplete(this.XmlHttp.responseText, this.XmlHttp.responseXML);
else
this.OnError(this.XmlHttp.status, this.XmlHttp.statusText, this.XmlHttp.responseText);
}
}

另外还有prototype.js 1.5
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步