我对 jquery 完全陌生。但想将其用于大学项目。我使用了网络服务。这些值是从 SQL 数据库中检索的。代码编译没有任何错误。但是自动完成功能不起作用。
处理程序的代码如下:
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Text;
public class Handler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string prefixText = context.Request.QueryString["q"];
string constr = "Data Source=USER\\SQLEXPRESS;Initial Catalog=my_db;Integrated Security=True;";
using (SqlConnection conn = new SqlConnection(constr))
{
// conn.ConnectionString = ConfigurationManager
// .ConnectionStrings["constr"].ConnectionString;
using (SqlCommand cmd = new SqlCommand(constr,conn))
{
cmd.CommandText = "select [name] from names where " +
"[name] like @SearchText + '%'";
cmd.Parameters.Add("@SearchText", SqlDbType.VarChar, 50).Value = prefixText + "%";
cmd.Connection = conn;
StringBuilder sb = new StringBuilder();
conn.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
sb.Append(sdr["name"])
.Append(Environment.NewLine);
}
}
conn.Close();
context.Response.Write(sb.ToString());
}
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
脚本代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Home.aspx.cs" Inherits="Home" %>
<!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 src="scripts/jquery-1.7.2.js" type="text/javascript"></script>
<script src="scripts/jquery-1.7.2.min.js" type="text/javascript"></script>
<script src="scripts/jquery.ui.autocomplete.js" type="text/javascript"></script>
<script src="scripts/jquery.ui.core.js" type="text/javascript"></script>
<script src="scripts/jquery.ui.position.js" type="text/javascript"></script>
<script src="scripts/jquery.ui.widget.js" type="text/javascript"></script>
<link href="scripts/demos.css" rel="stylesheet" type="text/css" />"
<script type="text/javascript">
$(document).ready(function () {
$("#<%= txtsearch.ClientID %>").autocomplete({
minLength: 1,
delay: 0,
"~/Handler.ashx"});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:TextBox ID="txtsearch" runat="server"></asp:TextBox>
</div>
</form>
</body>
</html>
有人可以告诉我我在哪里犯了错误。