参考站点:http://search.gongkong.com/search.aspx
suggest.js文件(在引入suggest.js文件前需要先引入jquery-1.4.2.min.js):
SearchCompany.ashx文件
public class SearchCompany : IHttpHandler {
public void ProcessRequest(HttpContext context)
{
string strconn = ConfigurationManager.ConnectionStrings["DBConn"].ConnectionString;
string keyword = context.Request.QueryString["cname"];
//string keycontext.Responseword = "河北";
//此处更改为您自己的sql或者存储过程
string sql = "select Member_TrueName from users where Member_TrueName like '%" + @keyword + "%' and Member_ShenFen=1 order by id desc";
//context.Response.Write(sql);
//context.Response.End();
SqlParameter spr = new SqlParameter(@keyword, SqlDbType.NVarChar, 50);
spr.Value = keyword;
SqlCommand cmd = new SqlCommand(sql);
context.Response.CacheControl = "no-cache";
context.Response.Clear();
context.Response.ContentType = "text/xml";
context.Response.Write("<?xml version=/"1.0/" encoding=/"GB2312/"?>");
context.Response.Write("<response>");
using (SqlConnection sqlconn = new SqlConnection(strconn))
{
sqlconn.Open();
cmd.Connection = sqlconn;
SqlDataReader dre = cmd.ExecuteReader();
if (dre.HasRows)
{
while (dre.Read())
{
context.Response.Write("<content>");
context.Response.Write("<name>" + dre.GetString(0) + "</name>");
context.Response.Write("</content>");
}
}
else
{
context.Response.Write("<content>");
context.Response.Write("<name>暂无查询数据</name>");
context.Response.Write("</content>");
}
context.Response.Write("</response>");
dre.Close();
}
}
public bool IsReusable {
get {
return false;
}
}
}
Default.aspx前台文件
<!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>
<mce:script src="jquery-1.4.2.min.js" mce_src="jquery-1.4.2.min.js" language="javascript" type="text/javascript"></mce:script>
<mce:script language="javascript" type="text/javascript" src="suggest.js" mce_src="suggest.js"></mce:script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td >
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<td><input id="FrmChangshang" name="FrmChangshang" type="text" style="width: 320px;" maxlength="20"/>
<div id="popup">
<table id="name_table" bgcolor="#FFFAFA" border="0" cellspacing="0" cellpadding="3px">
<tbody id="name_table_body"></tbody>
</table>
</div>
</td>
</tr>
</table>
<br />
<br />
<br />
</div>
</form>
</body>
</html>