62,074
社区成员
发帖
与我相关
我的任务
分享
funtion CreateXmlHttp()
{
var xmlHttp=null;
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
return xmlhttp;
}
function CheckUserName()
{
var xmlHttp=CreateXmlHttp()
http.open("GET","CheckUserName.ashx?username="+document.getElementById("txtUserName"),value, true);
http.onreadystatechange=ShowCheckResult;
http.send(null);
}
function ShowCheckResult()
{
if xmlHttp.readyState==400&& xmlHttp.stutas=200
{
var result=xmlHttp.responseText;
if(result==1)
alert("恭喜你 用户名可用")
else("异常遗憾,该名字已被注册");
}
}
<%@ WebHandler Language="C#" Class="CheckUserName" %>
using System;
using System.Web;
public class CheckUserName: IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/HTML";
if(UserNameIfExist())
context.Response.Write("1");
else
context.Response.Write("0");
}
protected bool UserNameIfExist()
{
string username = context.Request.QueryString["username"];
然后用username在数据库查询 ,返回结果
}
public bool IsReusable
{
get
{
return false;
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AjaxProTest.aspx.cs" Inherits="AJAXPro_Pro_AjaxProTest" %>
<!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" language="javascript">
function checkUserName(source)
{
var userName = source.value;
//注意:AJAXPro_Pro_AjaxProTest是AjaxProTest.aspx页面对应的class名称
//也就是在这里通过JavaScript调用服务器上的方法并接受返回的结果
var result = AJAXPro_Pro_AjaxProTest.checkName(userName).value;
document.getElementById("msg").innerHTML = result;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<ul style="margin-left:0px; list-style-type:none;">
<li>用户名:<input id="txtUserName" type="text" onblur="checkUserName(this)" /></li>
<li id="msg"></li>
<li>密码:<input id="txtPassword" type="password" /></li>
</ul>
</div>
</form>
</body>
</html>
using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AjaxPro;
public partial class AJAXPro_Pro_AjaxProTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//注册Type
AjaxPro.Utility.RegisterTypeForAjax(typeof(AJAXPro_Pro_AjaxProTest),this);
}
/// <summary>
/// 检查用户名
/// </summary>
/// <remarks>
/// 有AjaxPro调用的方法必须有AjaxMethod属性
/// </remarks>
/// <param name="userName">要检查的用户名</param>
/// <returns></returns>
[AjaxMethod]
public string checkName(string userName)
//这个方法是提供前台JavaScript调用的
{
bool exists = false;
if (userName.CompareTo("admin") == 0)
{
exists = true;
}
if (exists)
{
return string.Format("<font color='red'>{0}这个用户已经被注册</font>", userName);
}
else
{
return string.Format("<font color='green'>{0}这个用户暂未被注册</font>",userName);
}
}
}