当前位置:首页 > Asp.Net+Ajax 用户名检测

Asp.Net+Ajax 用户名检测

点击次数:995  更新日期:2010-12-29
\n

AJAX用户名检测


\n

前台的JS代码:
<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”);


\n

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>

前台的控件:
<asp:TextBox ID=”tb_UserName” runat=”server” TextMode=”SingleLine” Width=”180px” onBlur=”CheckUserName()”></asp:TextBox><span id=”CheckInfo”></span>

查询页checkusername.aspx的后台代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;


\n

public partial class checkusername : System.Web.UI.Page
{
private string sConnectionString = ConfigurationManager.ConnectionStrings["WebConnString"].ToString();
protected void Page_Load(object sender, EventArgs e)
{
string sUserName = Request.QueryString["username"].Trim().ToString();
using (SqlConnection conn = new SqlConnection(sConnectionString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(“SELECT COUNT(*) FROM tbWebManage WHERE UserName=@UserName”, conn))
{
cmd.Parameters.AddWithValue(“@UserName”, sUserName);
string sResult = Convert.ToString(cmd.ExecuteScalar());
Response.Clear();
if (sResult == “0″)
{
Response.Write(“1″);
}
else
{
Response.Write(“0″);
}
Response.End();
}
}
}


\n

}

来源:cnblogs

\n