AJAX 在 ASP.Net 中使用 jQuery 自动完成

AJAX autocomplete with jQuery in ASP.Net

我正在尝试使用 jQuery 在 ASP.Net 中实现 AJAX 自动完成。这是我的 table 定义:

CREATE TABLE [dbo].[tbluser](
    [nid] [int] IDENTITY(1,1) NOT NULL,
    [name] [varchar](100) NULL,
    [email] [varchar](100) NULL,
    [address] [varchar](200) NULL,
    [password] [varchar](100) NULL
)

我的网页即 Autocomplete.aspx

<head runat="server">
    <title></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>
        $(function () {
            $("#superheroz").autocomplete({    
                source: function (request, response) {
                    $.ajax({
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        url: '<%=ResolveUrl("Autocomplete.aspx/binddata") %>',
                        dataType: "json",
                        data: "{ 'name': '" + request.term + "'}",
                        success: function (data) {
                            response(data.d);
                        },
                        error: function (result) {
                            alert(response.responseText);
                        },
                        failure: function () { 
                            response.responseText   
                        }
                    });
                }
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <input type="text" id="superheroz" runat="server" />
        </div>
    </form>
</body>

这是我在 Autocomplete.aspx.cs

中创建的 Web 方法
[WebMethod]
public List<string> binddata(string name)
{
    List<string> ud = new List<string>();
    string sql = "select * from tbluser where name like '%" + name + "%'";
    ds = new DataSet();
    ds = da.getData(sql);
    if (ds.Tables[0].Rows.Count != 0)
    {
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            string su;
            su = dr["name"].ToString();
            ud.Add(su);
        }
    }

    return ud;
}

现在这会产生一个警告 'unknown'。请帮忙。

您在 aspx 中的 [WebMethod] 必须 static 才能工作!

url: '<%=ResolveUrl("Autocomplete.aspx/binddata") %>'

[WebMethod]
public static List<string> binddata(string name)
{
    //your code
}