我想在 asp.net 中使用数据库中的 jquery 自动完成功能。除了 minLength 选项外,我得到了它的工作。如以下代码所示,我尝试了几种方法,但无法使 minLength 选项起作用。请阐明这一点。谢谢你。
这是我的代码:
<script src="../../../jquery/jquery-1.7.2.js" type="text/javascript"></script>
<script type="text/javascript" src="../../../jquery/jquery.autocomplete.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#<%=TextBox1.ClientID%>").autocomplete({ minLength: 4 });
$("#<%=TextBox1.ClientID%>").autocomplete("option", "minLength", 4);
$("#<%=TextBox1.ClientID%>").autocomplete("AutocompleteData.ashx");
});
</script>
<%@ WebHandler Language="C#" Class="AutocompleteData" %>
using System;
using System.Web;
using System.Data.SqlClient;
public class AutocompleteData : IHttpHandler {
public void ProcessRequest(HttpContext context)
{
string schoolname = context.Request.QueryString["q"];
string sql = "my query";
using (SqlConnection connection = new SqlConnection(
System.Configuration.ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString))
using (SqlCommand command = new SqlCommand(sql, connection))
{
connection.Open();
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
context.Response.Write(reader.GetString(0) + Environment.NewLine);
}
}
}
}
public bool IsReusable {
get {
return false;
}
}
}