<body onclick="out()"> 搜索: <asp:TextBox ID="txtkey" onkeyup="update();" runat="server"></asp:TextBox> <asp:Button ID="btnKey" runat="server" Text="搜索" /> <div id="tisi"></div> #tisi { width: 190px; height: 200px; display: none; border: 1px solid #ccc; margin-left: 52px; z-index: 999; position: absolute; background-color:White; } <script src="JS/jquery-1.6.1.min.js" type="text/javascript"></script> <script type="text/javascript" language="javascript"> /* ajax异步获取数据 */ function update() { $("#tisi").show(); //显示div var key = $("#<%=txtkey.ClientID %>").val();//获取文本框的值 var url = "gettisi.ashx?key=" + encodeURI(key) + "&t=" + new Date().valueOf();//通过url传入编码的key并加上时间 $.get(url, function (data) {//jquery的get方法返回获取的值 $("#tisi").html(data); }); } /* 鼠标移到ITEM上时有背景颜色 */ function itemover(obj) { $(obj).css("backgroundColor", "#ccc"); } /* 鼠标移出时背景变白 */ function itemout(obj) { $(obj).css("backgroundColor", ""); } /* 设置文本框的值 */ function settxt(obj) { $("#<%=txtkey.ClientID %>").val($(obj).text());//把获取的值返回到文本框中 $("#tisi").hide();//隐藏div } /* 单击body的其它区域时 */ function out() { $("#tisi").hide(); } </script>
gettisi.ashx一般处理程序
public void ProcessRequest(HttpContext context) { string key = context.Request.QueryString["key"]; if (string.IsNullOrEmpty(key)) { context.Response.Write(""); } else { key = context.Server.UrlDecode(key); DataSet ds = new DAL.LinkDAL_own().GetLikelinkname(key); //DataSet ds = new DAL.LinkDAL().GetLikelinkname(key); DataTable dt = ds.Tables[0]; StringBuilder sb = new StringBuilder(); foreach (DataRow item in dt.Rows) { sb.Append("<div class='item' onmouseover='itemover(this)' onmouseout='itemout(this)' onclick='settxt(this)'>" + item["linkname"] + "</div>"); } context.Response.Write(sb.ToString()); } }
DAL层中的代码
public DataSet GetLikelinkname(string key) { string sql = "select top 10 linkname from test_link where linkname like '%" + key + "%'"; Database db = DatabaseFactory.CreateDatabase(); DbCommand dbCommand = db.GetSqlStringCommand(sql); return db.ExecuteDataSet(dbCommand); }
自定义操作类的DAL层中代码
public DataSet GetLikelinkname(string key) { string sql = "select top 10 linkname from test_link where linkname like '%" + key + "%'"; MSSQLHelper h = new MSSQLHelper(); h.CreateCommand(sql); DataTable dt = h.ExecuteQuery(); DataSet ds = new DataSet(); ds.Tables.Add(dt); return ds; }