How can we integrate jQuery autocomplete using asp.net, webservice and sql database?

前端 未结 2 519
名媛妹妹
名媛妹妹 2021-01-07 08:41

I am trying to implement the code given for \"jQuery Autocomplete and ASP.NET\", but unable to integrate it because you are using subsonic to query database.

So can

2条回答
  •  我在风中等你
    2021-01-07 09:37

    This is a pretty easy task, the catch is that the jQuery autocomplete extender expects an array of values. Here is example of how I parse the standard XML results from a ASMX web serivce to use with the jQuery autocomplete extender.

    Since ASP.NET likes to rewrite your ID's, you can pass in the ClientID to get the dynamic ID.

        $("#<%= TextBox1.ClientID %>").autocomplete("/Demo/WebSvc.asmx/SuggestCustomers", {
            parse: function(data) {
                var parsed = [];
    
                $(data).find("string").each(function() {
                    parsed[parsed.length] = {
                        data: [$(this).text()],
                        value: $(this).text(),
                        result: [$(this).text()]
                    };
                });
                return parsed;
            },
            dataType: "xml"
        });
    

    Here is what the associated web service would look like, remember to uncomment the [ScriptService] attribute on the web service:

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ScriptService]
    public class WebSvc: WebService
    {
        [WebMethod]
        public string[] SuggestedCustomers(string q)
        {
            // Do Query
    
            // Add items into string array
            List items = new List();
            while (dr.Read())
            {
                items.Add(dr[0].ToString());
            }
    
            // Return array
            return items.ToArray();
        }
    
    }
    

提交回复
热议问题