这个是前台显示的页面代码↓
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!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 language="javascript" type="text/javascript"> function neihe() {//设置浏览器的内核 if (window.ActiveXObject) {//如果是IE浏览器内核 return new ActiveXObject("Microsoft.XMLHTTP"); } else if (window.XMLHttpRequest) {//否则 return new XMLHttpRequest(); } } var XMLduixiang; function fasong(name) { if (name != "") { var url = encodeURI("houtai.aspx?chuandi=" + name); //encodeURI对URL编码 XMLduixiang = neihe(); //创建XMLGTttpRequest XMLduixiang.onreadystatechange = huidiao; //回传处理结果(请求被发送到服务器时) XMLduixiang.open("GET", url, true); //初始化XMLGTttpRequest组件 //最后的true(异步)或 false(同步) XMLduixiang.send(null);//发送请求 } } function huidiao(){ if(XMLduixiang.readyState==4 && XMLduixiang.status == 200){ var cljg = XMLduixiang.responseText; //responseText获得字符串形式的响应数据 //responseXML获得 XML 形式的响应数据 if(cljg=="true"){//如果返回真 document.getElementById("ts").style.display = "none"; } else {//否则 document.getElementById("ts").style.display = "inline"; } } } </script> </head> <body> <form id="form1" runat="server"> <div> 输入用户名: <input type="text" onblur="fasong(this.value)" /><%--失去焦点--%> <span id="ts" style=" display:none; color:Red;">没有这个用户</span> </div> </form> </body> </html>
这个是后台页面的代码↓
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Configuration; public partial class houtai : System.Web.UI.Page { string sjklj = ConfigurationManager.ConnectionStrings["ceshi"].ConnectionString; protected void Page_Load(object sender, EventArgs e) { Response.ContentType = "text/plain";//设置输出类型 if (Request.QueryString["chuandi"] != null) { string name = Request.QueryString["chuandi"].ToString().Trim(); if (name.Length>0) { if (shifou(name)) { Response.Write("true"); } else { Response.Write("false"); } } else { Response.Write("false"); } } else { Response.Write("false"); } } protected bool shifou(string name) { string sql = "select * from cs where name=@name"; using (SqlConnection conn = new SqlConnection(sjklj)) { conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn); cmd.Parameters.AddRange(new SqlParameter[] { new SqlParameter("@name", name) }); if (cmd.ExecuteScalar() == null) { return false; } else { return true; } } } }