2

I have Jquery autocomplete working with a HttpHandler - .ashx file. It works fine, I am wondering is there an easy way to use the autocomplete with a [WebMethod] right in the code behind - and are there any advantages to this?

1 Answer 1

6

Both implementations of HttpHandler and Web-Services perform identically,
however I prefer HttpHandler as it is lightweight,
on the other side a web-service encodes request & response xml data which adds extra payload.

POP JqueryUI Autocomplete with web-methods:
http://blog.nitinsawant.com/2011/09/integrating-jquery-ui-autocomplete-in.html

JS:

$(document).ready(function () {
            $("#<%=txtAutoComplete.ClientID %>").autocomplete({
                source: function (request, response) {
                    $.ajax({
                        url: "webservice/TestService.asmx/SearchData",
                        data: "{ 'q': '" + request.term + "', 'limit': '10' }",
                        dataType: "json",
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        dataFilter: function (data) { return data; },
                        success: function (data) {
                            response($.map(data.d, function (item) {
                                return {
                                    label: item.Name,
                                    value: item.id + ""
                                }
                            }))
                        }
                    });
                }
            });
});


C#:

[System.Web.Services.WebMethodAttribute(),  System.Web.Script.Services.ScriptMethodAttribute()]
public List<tdata> SearchData(string q, int limit)
{
    return new List<tdata> { new tdata { id = 0, name = "nitin" } };
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.