AutoComplete JQuery插件和ASP.Net C#

时间:2009-10-26 20:17:50

标签: c# asp.net jquery-plugins

我正在使用JQuery自动完成插件,并传入一个字符串数组以自动完成(请参阅下面的代码)。我正在调用获取数据的方法(GetCustomerNames)只是返回一个字符串数组,这很正常。我需要找到一些方法将参数传递给GetCustomerNames方法,以便我可以过滤返回的内容。任何人都可以帮忙吗?

以下是Default.aspx页面中的标记代码:

<head runat="server">
<title></title>
<script type="text/javascript" src="js/jquery-1.3.2.js" ></script>
<script type="text/javascript" src="js/jquery.autocomplete.js" ></script>  
<script type="text/javascript">
    //Working, but uses results output to an aspx page using StringBuilder, trying
    //to find a way to get the data with json
    //$(document).ready(function() {
      //  $("#example").autocomplete('AutoCompleteData.aspx');

    //});
    $(document).ready(function() {
        $("#example").keyup(function() {
            $.ajax({
                type: "POST",
                url: "Default.aspx/GetCustomerNames",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(msg) {    
                    $("#example").autocomplete(msg.d);
                },
                error: function(msg) {
                    alert("error");
                }
            });    
        });
    });    
</script>

         
        顾客姓名:          

以下是实现GetCustomerNames方法的Default.aspx.cs代码隐藏页面中的代码:

[WebMethod]
public static string[] GetCustomerNames()
{
    string[] data = new string[] {"Andrew", "Ramona", "Russ", "Russell", "Raymond"};

    return data;

}

1 个答案:

答案 0 :(得分:1)

您可以使用data哈希将参数传递给方法:

$.ajax({
    type: 'POST',
    url: 'Default.aspx/GetCustomerNames',
    data: '{ parameterName: "some test value" }',
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    success: function(msg) {
        $("#example").autocomplete(msg.d);
    },
    error: function(msg) {
        alert("error");
    }
});

您的网络方法变为:

public static string[] GetCustomerNames(string parameterName)