AJAX+C# 验证用户是否被注册
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AJAX.aspx.cs" Inherits="ToImage" %>
<!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">
var httprequest=false;
function sendRequest(url)
{
if(window.XMLHttpRequest)
{
httprequest=new XMLHttpRequest();
if(httprequest.overrideMimeType)
{
httprequest.overrideMimeType("text/xml");
}
}
else if(window.ActiveXObject)
{
try
{
httprequest=new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
httprequest=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){}
}
}
if(!httprequest)
{
window.alert("不能创建XMLHttpRequest对象实例");
return false;
}
httprequest.onreadystatechange=processRequest;
httprequest.open("GET",url,true)
httprequest.send(null);
}
function processRequest()
{ var divcheck=document.getElementById("checkinfo");
if(httprequest.readyState==4)
{
if(httprequest.status==200)
{
if(httprequest.responseXML.text=="True")
{
divcheck.style.color='blue';
divcheck.innerText="该用户名未被使用过,可以继续";
}
else if(httprequest.responseXML.text=="False")
{
divcheck.style.color='red';
divcheck.innerText="该用户名以被注册过,请换一个用户名";
}
else
{
divcheck.innerText="error!";
}
}
else
{
divcheck.innerText="error!";
}
}
}
function userCheck(url)
{
var f=document.form1;
var username=f.Username.value;
if(username=="")
{
var divcheck=document.getElementById("checkinfo");
divcheck.style.color='red'
divcheck.innerText="用户名不能为空";
f.Username.focus();
return false;
}
else
{
url+=f.Username.value;
sendRequest(url);
return false;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<br />
<br />
<table style="width: 398px; height: 53px">
<tr>
<td style="width: 67px; height: 8px; text-align: center;">
用户名:</td>
<td style="width: 13px; height: 8px; text-align: center;">
<asp:TextBox ID="Username" runat="server"></asp:TextBox>
</td>
<td style="width: 55px; height: 8px;">
<asp:LinkButton ID="LinkButton1" runat="server" Width="147px" >检查用户名的唯一性</asp:LinkButton>
<div id="checkinfo" style="width: 138px; position: static; height: 35px; left: 31px; top: 171px;" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
<!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">
var httprequest=false;
function sendRequest(url)
{
if(window.XMLHttpRequest)
{
httprequest=new XMLHttpRequest();
if(httprequest.overrideMimeType)
{
httprequest.overrideMimeType("text/xml");
}
}
else if(window.ActiveXObject)
{
try
{
httprequest=new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
httprequest=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){}
}
}
if(!httprequest)
{
window.alert("不能创建XMLHttpRequest对象实例");
return false;
}
httprequest.onreadystatechange=processRequest;
httprequest.open("GET",url,true)
httprequest.send(null);
}
function processRequest()
{ var divcheck=document.getElementById("checkinfo");
if(httprequest.readyState==4)
{
if(httprequest.status==200)
{
if(httprequest.responseXML.text=="True")
{
divcheck.style.color='blue';
divcheck.innerText="该用户名未被使用过,可以继续";
}
else if(httprequest.responseXML.text=="False")
{
divcheck.style.color='red';
divcheck.innerText="该用户名以被注册过,请换一个用户名";
}
else
{
divcheck.innerText="error!";
}
}
else
{
divcheck.innerText="error!";
}
}
}
function userCheck(url)
{
var f=document.form1;
var username=f.Username.value;
if(username=="")
{
var divcheck=document.getElementById("checkinfo");
divcheck.style.color='red'
divcheck.innerText="用户名不能为空";
f.Username.focus();
return false;
}
else
{
url+=f.Username.value;
sendRequest(url);
return false;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<br />
<br />
<table style="width: 398px; height: 53px">
<tr>
<td style="width: 67px; height: 8px; text-align: center;">
用户名:</td>
<td style="width: 13px; height: 8px; text-align: center;">
<asp:TextBox ID="Username" runat="server"></asp:TextBox>
</td>
<td style="width: 55px; height: 8px;">
<asp:LinkButton ID="LinkButton1" runat="server" Width="147px" >检查用户名的唯一性</asp:LinkButton>
<div id="checkinfo" style="width: 138px; position: static; height: 35px; left: 31px; top: 171px;" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
public partial class AJAX : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string url = Request.Url.ToString();
url = url.Substring(0, url.LastIndexOf('/') + 1) + "CheckUser.aspx?CheckUser=";
LinkButton1.Attributes.Add("onClick", "return userCheck('"+url+"');");
}
}
{
protected void Page_Load(object sender, EventArgs e)
{
string url = Request.Url.ToString();
url = url.Substring(0, url.LastIndexOf('/') + 1) + "CheckUser.aspx?CheckUser=";
LinkButton1.Attributes.Add("onClick", "return userCheck('"+url+"');");
}
}
public partial class CheckUser : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string NewUser= Request.QueryString["CheckUser"].ToString();
bool send= IsUser(NewUser);
Page.Response.ContentType = "text/xml";
Response.Write("<IsUser>"+send+"</IsUser>");
Response.End();
}
private bool IsUser(newuser)
{
//
//读取数据库或数据源判断用户名是否存在
//
}
{
protected void Page_Load(object sender, EventArgs e)
{
string NewUser= Request.QueryString["CheckUser"].ToString();
bool send= IsUser(NewUser);
Page.Response.ContentType = "text/xml";
Response.Write("<IsUser>"+send+"</IsUser>");
Response.End();
}
private bool IsUser(newuser)
{
//
//读取数据库或数据源判断用户名是否存在
//
}