-1

我对 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>

有人可以告诉我我在哪里犯了错误。

4

2 回答 2

0

试试这一行:

 "~/Handler.ashx"});  

没有 ~ 使用 fiddler (http://www.fiddler2.com/fiddler2/) 查看请求是否发送

于 2012-05-18T10:38:59.270 回答
0

您初始化自动完成的方式不正确。您必须为source您的处理程序设置选项:

$("#<%= txtsearch.ClientID %>").autocomplete({
    source: "Handler.ashx",
    minLength: 1,
    delay: 0
});

此外,自动完成功能会将要搜索的值作为 querystring key 发送term,而不是q. 因此,在您的处理程序中,您可以通过以下方式获得价值:

string prefixText = context.Request.QueryString["term"];
于 2012-05-18T10:39:18.267 回答