预测搜索不起作用?

时间:2012-08-04 06:46:42

标签: c# asp.net search textbox

这是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)。当我在文本框中输入任何内容并按下按钮时,搜索不起作用。 一切似乎都是正确的,但不知道错误在哪里。

帮助朋友。

由于

1 个答案:

答案 0 :(得分:0)

问题在于这行代码

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

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

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

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