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

//第一步:新建一个(*.aspx|*.html)Index.aspx页面 添加jquery
1
<html xmlns="http://www.w3.org/1999/xhtml"> 2 <head runat="server"> 3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 4 <title>检测用户名是否存在</title> 5 <script type="text/javascript" src="js/jquery-1.9.1.js"></script> 6 <script type="text/javascript"> 7 function UserName() { 8 $.ajax({ 9 type: "GET", 10 url: "Index.ashx", 11 dataType: "html", 12 13 data: "userName=" + $("#txtName").val(), 14 beforeSend: function (XMLHttpRequest) { 15 $("#showResult").text("正在查询..."); 16 }, 17 success: function (msg) { 18 $("#showResult").html(msg); 19 $("#showResult").css("color", "red"); 20 }, 21 complete: function (XMLHttpRequest, textStatus) { 22 //隐藏正在查询图片 23 }, 24 error: function () { 25 //错误处理 26 } 27 }); 28 } 29 30 </script> 31 32 </head> 33 <body> 34 <form id="form1" runat="server"> 35 <div> 36 <input id="txtName" type="text" /><input type="button" value="检测" id="btn" onclick="UserName();" /> 37 <div id="showResult" style="float: left"></div> 38 </div> 39 </form> 40 </body> 41 </html>


第二步:新建一个处理界面Index.ashx
 1 public void ProcessRequest(HttpContext context)
 2         {
 3             context.Response.ContentType = "text/html";
 4             string userName = context.Request.QueryString["userName"].Trim().ToString();
 5             DataTable dt = SqlHelper.ExecuteDataTable("select  *  from  dbo.T_Login  where UseName=@UseName", 
new SqlParameter("@UseName", SqlDbType.NVarChar) { Value = userName }); 6 //判断表不能为空 7 DataRow dr = null; 8 if (dt != null && dt.Rows.Count > 0) 9 { dr = dt.Rows[0]; 10 if (userName ==dr["UseName"].ToString()) 11 { 12 context.Response.Write("用户名已经存在!"); 13 } 14 else 15 { 16 context.Response.Write("您可以使用此用户名!"); 17 } 18 } 19 else 20 { 21 context.Response.Write("您可以使用此用户名!"); 22 } 23 }
 
原文地址:https://www.cnblogs.com/ljs05/p/3432105.html