<%@ WebHandler Language="C#" Class="Search_CS" %>
using System;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
using System.Text;
public class Search_CS : IHttpHandler {
ClassData clsDt = new ClassData();
public void ProcessRequest (HttpContext context) {
string prefixText = context.Request.QueryString["prefixText"];
string kk = clsDt.getDataTable("select ContactName from Customers where " + "ContactName like @SearchText + '%'").Rows[0][0].ToString();
using (SqlCommand cmd = new SqlCommand())
{
switch (prefixText)
{
case "1":
//tb1
cmd.CommandText = kk;
break;
case "2":
//tb2
cmd.CommandText = kk;
break;
}
//cmd.CommandText = "select ContactName from Customers where " + "ContactName like @SearchText + '%'";
cmd.Parameters.AddWithValue("@SearchText", prefixText);
cmd.Connection = clsDt.sqlCnn;
StringBuilder sb = new StringBuilder();
clsDt.sqlCnn.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
sb.Append(sdr["ContactName"])
.Append(Environment.NewLine);
}
}
clsDt.sqlCnn.Close();
context.Response.Write(sb.ToString());
}
}
public bool IsReusable {
get {
return false;
}
}
}
私はファイルashxを持っています..オートコンプリートプラグインjqueryを使用して、テキストボックスに提案を入力したい...
<script src="js/jquery-1.7.2.min.js" type="text/javascript"></script>
<script src="js/jquery.autocomplete.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#txtSearch").autocomplete('Search_CS.ashx?prefixText=1');
$("#TextBox1").autocomplete('Search_CS.ashx?prefixText=2');
});
</script>
上記はjqueryクライアント側です
ashx ファイルに問題があります...