【问题标题】:Predictive search isn't working?预测搜索不起作用?
【发布时间】:2012-08-04 06:46:41
【问题描述】:

这是aspx文件代码。

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>AutoComplete Box with jQuery</title>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script> 
<script type="text/javascript">
    $(document).ready(function () {
        SearchText();
    });
    function SearchText() {
        $(".autosuggest").autocomplete({
            source: function (request, response) {
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "Default.aspx/GetAutoCompleteData",
                    data: "{'username':'" + document.getElementById('txtSearch').value + "'}",
                    dataType: "json",
                    success: function (data) {
                        response(data.d);
                    },
                    error: function (result) {
                        alert("Error");
                    }
                });
            }
        });
    }
</script>
</head>
<body>
<form id="form1" runat="server">
<div class="demo"></div>
<div class="ui-widget">
<label for="tbAuto">Enter UserName: </label>
<input type="text" id="txtSearch" class="autosuggest" />
</div>
</form>
</body>
</html>

这是.cs文件代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Web.Services;

public partial class demo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
     public static string getconnectionstring()
    {
        return System.Configuration.ConfigurationManager.ConnectionStrings["crudconnection"].ConnectionString;
    }
    public static List<string> GetAutoCompleteData(string username)
    {
        List<string> result = new List<string>();
        SqlConnection con = new SqlConnection(getconnectionstring();

            using (SqlCommand cmd = new SqlCommand("select DISTINCT username from crudtable where username LIKE '%'+@SearchText+'%'", con))
            {
                con.Open();
                cmd.Parameters.AddWithValue("@SearchText", username);
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    result.Add(dr["username"].ToString());
                }
                return result;
            }

    }
}

数据库“crud”具有“crudtable”列(用户名、名字、姓氏、地址、id)。当我在文本框中输入任何内容并按下按钮时,搜索不起作用。 一切似乎都是正确的,但不知道错误在哪里。

帮助朋友。

谢谢

【问题讨论】:

    标签: c# asp.net search textbox


    【解决方案1】:

    问题出在这行代码

    cmd.Parameters.AddWithValue("@SearchText", username);
    

    不要在参数名称中添加@。使用

    cmd.Parameters.AddWithValue("SearchText", username);
    

    第二次添加断点并检查您的代码是否被 ajax 调用命中。

    【讨论】:

    • 我删除了@,但它仍然不起作用。我还在 $(document).ready(function () { SearchText(); }); 处添加了断点。但是当我在框中输入一些文本并按下回车时,控制没有去那里。感谢您的回复。
    • 使用网络嗅探器如 fiddler 来查看在文本框中输入时是否进行了 ajax 调用。您也可以在 firefox 中使用(并且更好)firebug。打开 firebug,转到 Net 选项卡并在子选项卡中转到 XHR 并查看它是否显示任何 ajax 活动。如果它在那里,那么看看什么是请求,什么是响应。
    猜你喜欢
    • 1970-01-01
    • 2015-02-10
    • 2013-02-04
    • 2016-01-06
    • 2016-01-23
    • 2017-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多