页面代码:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function $(e) {
return document.getElementById ? document.getElementById(e) : null;
}
function Check(name) {
var xmlHttp = false;
var url = "Handler1.ashx?name=" + name + "&rd=" + Math.random();
CreateXmlHttp();
ajax_process(url);
}
function ajax_process(url) {
xmlHttp.open("post", url, true);
xmlHttp.onreadystatechange = ajax_sumbitemployment;
xmlHttp.send(null);
function ajax_sumbitemployment() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
if (xmlHttp.responseText == "validateNull") {
$('Label1').innerText = " 用户名不能为空!";
return;
}
if (xmlHttp.responseText == "smallLength") {
// $('Label1').innerText = " 不能小于2个字符!";
alert("5432323");
return;
}
if (xmlHttp.responseText == "exist") {
$('Label1').innerText = " 用户名已存在,请另选用户名!";
return;
}
else {
$('Label1').innerText = "恭喜您用户名可以使用!";
}
}
}
}
}
//创建Ajax对象
function CreateXmlHttp() {
try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); }
catch (e) {
try { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); }
catch (e) {
try { xmlHttp = new XMLHttpRequest(); }
catch (e) { xmlHttp = false; }
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:TextBox ID="txtUsernaem" runat="server" onblur="return Check(this.value)"></asp:TextBox>
<asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>
</div>
</form>
</body>
</html>
处理文件:Handler1.ashx
public class Handler1 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
string Name = context.Request.QueryString["Name"];
if (!String.IsNullOrEmpty(Name))
{
if (Name.Length < 2)
{
context.Response.Write("smallLength");
}
else
{
context.Response.Write("exist");
}
// Maticsoft.BLL.Personnel person = new Maticsoft.BLL.Personnel();
// Maticsoft.Model.personnelDate personDate = person.GetModel(Name);
//if (personDate != null)
//{
// context.Response.Write("exist");
//}
}
else
{
context.Response.Write("validateNull");
}
}
public bool IsReusable
{
get
{
return false;
}
}
}