简体   繁体   English

AutoComplete JQuery插件和ASP.Net C#

[英]AutoComplete JQuery plugin and ASP.Net C#

I'm using the JQuery autocomplete plug in, and am passing in an array of strings to autocomplete (see code below). 我使用的是JQuery自动完成功能插件,并传递了一个字符串数组来自动完成功能(请参见下面的代码)。 The method I'm calling to get my data (GetCustomerNames) is just returning an array of strings, and that is working fine. 我正在调用的用于获取数据的方法(GetCustomerNames)仅返回一个字符串数组,并且工作正常。 I need to find some way to pass in a parameter to the GetCustomerNames Method so I can filter what is being returned. 我需要找到一种将参数传递给GetCustomerNames方法的方法,以便可以过滤返回的内容。 Can anyone help with this? 有人能帮忙吗?

Here is the markup code in the Default.aspx page: 这是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>


Customer Name: 顾客姓名:

And here is the code in the Default.aspx.cs code behind page implementing the GetCustomerNames method: 以下是实现GetCustomerNames方法的页面后面Default.aspx.cs代码中的代码:

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

    return data;

}

You could use the data hash to pass parameters to the method: 您可以使用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");
    }
});

And your web method becomes: 您的网络方法变为:

public static string[] GetCustomerNames(string parameterName)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM