Paul
Paul

Reputation: 3142

ASP.NET Jquery Autocomplete

I have an ASP.NET webforms application and trying to implement jQuery autocomplate on a text box. The server code is getting called, but nothing is displayed. I've replaced the call to the web service and added some static text and that gets displayed OK. Can anyone see what the issue is?

The server side code is here:

[WebMethod]
    public string[] ReturnPostcodes(string term)
    {
        PostcodeService postcodes = new PostcodeService();
        var results = postcodes.ReturnPostcodes().Where(p => p.Postcode.StartsWith(term.ToUpper())).Select(p => p.Postcode).Take(20).ToArray();
        return results;

    }

The HTML is here:

<tr>
        <td>Mobile Telephone:</td>
        <td><asp:TextBox runat="server" ID="txtPostcode"></asp:TextBox></td>
    </tr>

The jquery is here:

$(document).ready(function () {
        $('#ctl00_ctl00_mainContent_mainContent_txtPostcode').each(function () {

            $(this).autocomplete({
                 source: '/Postcodes.asmx/ReturnPostcodes'
            });
        });
    });

Upvotes: 3

Views: 627

Answers (3)

thiagoleite
thiagoleite

Reputation: 503

The reason nothing is showing up is because you are not giving autocomplete the correct data format, also you are not specifying it how to load the data. You can return JSON or XML format from webservice or parse the response yourself using jquery. Checkout jquery ui autocomplete site for Remote JSONP and XML data parsed once examples.

Upvotes: 3

Guillaume86
Guillaume86

Reputation: 14400

asp.net WebMethods returns a json object containing the response in a 'd' variable (and there's some options to set when you call it with jQuery XHR), script snippet:

 <script type="text/javascript">
        $(function () {
            var lastXhr, cache = {};

            $('#<%= Search.ClientID %>').autocomplete({
                source: function (request, response) {
                    var term = request.term;
                    if (term in cache) {
                        response(cache[term]);
                        return;
                    }

                    lastXhr = $.ajax({
                        type: "POST",
                        url: "Default.aspx/GetBooks",
                        data: "{ \"term\": \"" + request.term + "\" }",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (data, status, xhr) {
                            cache[term] = data.d;
                            if (xhr === lastXhr) {
                                response(data.d);
                            }
                        }
                    });
                }
            });
        });
    </script>

Upvotes: 0

Massimiliano Peluso
Massimiliano Peluso

Reputation: 26727

Make sure that Result is not empty

Try using StartsWith and pass in a StringComparison

  var results = postcodes.ReturnPostcodes().Where(p => p.Postcode.StartsWith(term,StringComparison.InvariantCultureIgnoreCase)).Select(p => p.Postcode).Take(20).ToArray();

Upvotes: 0

Related Questions