62,074
社区成员
发帖
与我相关
我的任务
分享
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script>
function openAjax() {
var val = document.getElementById('txt').value;
var url="VerifyUserNameHandler.ashx?para=" + val + "&date=" + new Date();
$.get(url, function(d) {
$("#resultSpan").html(d);
})
}
</script>
</head>
<body>
<form id="form1" runat="server">
用户名:<input type="text" id='txt' value="Sandy" onblur="openAjax();" /> <span id="resultSpan"></span>
</form>
</body>
</html>
<%@ WebHandler Language="C#" Class="VerifyUserNameHandler" %>
using System;
using System.Web;
using System.Collections;
using System.Collections.Generic;
public class VerifyUserNameHandler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
//context.Response.ContentType = "text/plain";
string _name = context.Request.QueryString["para"];
_name = string.IsNullOrEmpty(_name) ? "" : _name;
System.Threading.Thread.Sleep(3000);//用线程来模拟数据库查询工作
string[] Names = new string[] { "Sandy", "阿非", "abc" };//这里用Names数组来代替数据库中的结果集
if (Array.IndexOf<string>(Names, _name) == -1)
{
context.Response.Write("恭喜,用户名可以使用。");
}
else
{
context.Response.Write("抱歉,用户名已被使用。");
}
}
public bool IsReusable {
get {
return false;
}
}
}