Asp.Net+Ajax 用户名检测
前台
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script language="javascript" type="text/javascript">
function CheckUserName()
{
var sUserName=document.getElementById("<%= tb_UserName.ClientID%>");
var sCheckInfo=document.getElementById("CheckInfo");
sCheckInfo.innerText="查询中……";
var objxml = new ActiveXObject("Microsoft.XMLHTTP");
objxml.open("GET","checkusername.aspx?username=" + sUserName.value,false);
objxml.send();
var sResult=objxml.responseText;
if(objxml.status=="200")
{
switch(sResult)
{
case "1":
sCheckInfo.innerHTML="<font color=red>恭喜</font>," + sUserName.value + "可以使用";
break;
case "0":
sCheckInfo.innerHTML="<font color=red>抱歉</font>,用户名" + sUserName.value + "已经被使用";
break;
}
}
else
{
sCheckInfo.innerHTML="<font color=red>抱歉</font>,查询失败";
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="tb_UserName" runat="server" TextMode="SingleLine" Width="180px" onBlur="CheckUserName()"></asp:TextBox><span id="CheckInfo"></span>
<br />
<asp:TextBox ID="TextBox1" runat="server" Width="179px"></asp:TextBox></div>
</form>
</body>
</html>
后台 check.aspx
public partial class checkusername : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string sUserName = Request.QueryString["username"].Trim().ToString();
DataSet ds=new DataSet();
ds = Maticsoft.DBUtility.DbHelperSQL.Query("SELECT count(*) from TB_USER where User_Name='"+sUserName+"'");
int userCount = int.Parse(ds.Tables[0].Rows[0][0].ToString());
Response.Clear();
if (userCount == 0)
{
Response.Write("1");
}
else
{
Response.Write("0");
}
Response.End();
}
}