.net中使用JQuery Ajax判断用户名是否存在的方法

时间:2023-03-09 06:03:52
.net中使用JQuery Ajax判断用户名是否存在的方法
//第一步:新建一个(*.aspx|*.html)Index.aspx页面 添加jquery
1 <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>检测用户名是否存在</title>
<script type="text/javascript" src="js/jquery-1.9.1.js"></script>
<script type="text/javascript">
function UserName() {
$.ajax({
type: "GET",
url: "Index.ashx",
dataType: "html", data: "userName=" + $("#txtName").val(),
beforeSend: function (XMLHttpRequest) {
$("#showResult").text("正在查询...");
},
success: function (msg) {
$("#showResult").html(msg);
$("#showResult").css("color", "red");
},
complete: function (XMLHttpRequest, textStatus) {
//隐藏正在查询图片
},
error: function () {
//错误处理
}
});
} </script> </head>
<body>
<form id="form1" runat="server">
<div>
<input id="txtName" type="text" /><input type="button" value="检测" id="btn" onclick="UserName();" />
<div id="showResult" style="float: left"></div>
</div>
</form>
</body>
</html> 第二步:新建一个处理界面Index.ashx
 public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
string userName = context.Request.QueryString["userName"].Trim().ToString();
DataTable dt = SqlHelper.ExecuteDataTable("select * from dbo.T_Login where UseName=@UseName",
new SqlParameter("@UseName", SqlDbType.NVarChar) { Value = userName });
//判断表不能为空
DataRow dr = null;
if (dt != null && dt.Rows.Count > )
{ dr = dt.Rows[];
if (userName ==dr["UseName"].ToString())
{
context.Response.Write("用户名已经存在!");
}
else
{
context.Response.Write("您可以使用此用户名!");
}
}
else
{
context.Response.Write("您可以使用此用户名!");
}
}