<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ajaxForm1.aspx.cs" Inherits="test2_ajaxForm1" %>
<!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 src="../js/Demonstrate/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="text" value="" id="userName" οnblur="CheckUsername();"/>
<span id="spanName" style="color:Red"></span>
</div>
</form>
</body>
</html>
<script type="text/javascript">
//验证是否已经注册用户 encodeURI 解决中文问题
function CheckUsername() {
var strName = $("#userName").val();
var flag = true;
$.ajax({
type: "GET",
async: false,
url: "../Ashx/IsName.ashx?userName=" + encodeURI(strName) + "&date=" + new Date(),
success: function(msg) {
if (msg == "1") {
$("#spanName").html("已存在");
flag = false;
}
else {
$("#spanName").html("可以使用");
}
}
});
return flag;
}
</script>
<%@ WebHandler Language="C#" Class="IsName" %>
using System;
using System.Web;
public class IsName : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
string res = string.Empty;
string strName = context.Request.QueryString["userName"];
if (!IsNName(strName))
{
res = "1";
}
else
{
res = "0";
}
context.Response.Write(res);
context.Response.End();
}
private bool IsNName(string strName)
{
bool bRet;
//模拟一个数据
string strUserName = "张三";
if (strName != strUserName)
{
bRet = true;
}
else
{
bRet = false;
}
return bRet;
}
public bool IsReusable {
get {
return false;
}
}
}