AutoComplete Not firing

It all looks good below I have provided the 3 parts the service works fine if queried.

/// <summary>
/// Summary description for AutoCompleteService
/// </summary>
[WebService(Namespace = "http://schemas")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class AutoCompleteService : BaseWebService
{
    /// <summary>
    /// Gets or sets the account finder service.
    /// </summary>
    public ICheckoutService CheckOutService { get; set; }

    /// <summary>
    /// Finds all addresses matching either the postcode or suburb given in the prefix text.
    /// </summary>
    /// <param name="prefixText">The prefix text.</param>
    /// <param name="count">The count.</param>
    /// <returns>Aray of address details</returns>
    [WebMethod]
    public string[] FindAddresses(string prefixText, int count)
    {
        //Check the parameters
        if (count == 0) count = 10;
        var suburbs = CheckOutService.FindMatchingForPartialAddress(prefixText);
        return suburbs.Take(count).ToArray();


    }
}

Followed by javascript

<script language="javascript">


$(function () {
    $("#postcode").autocomplete({
        source: "AutoCompleteService.asmx/FindAddresses",
        minLength: 1,
        select: function (event, ui) {
            $(this).val(ui.item.value);
        }
    });
});
</script>

Followed by textbox

     <asp:TextBox runat="server" name="postcode" id="postcode" ></asp:TextBox>

Response

 <ArrayOfStringxmlns: xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:    xsd="http://www.w3.org/2001/XMLSchema"xmlns="http://schemas.forcetechnology.com.au/2009/04/Fulfilment"><string>ABBARIVER,WA,6280</string><string>ABBEY,WA,6280</string><string>ACTONPARK,WA,6280</string>string>ADAMSVALE,WA,6375</string></ArrayOfString>

OKay I made the following alterations thankyou all for pointing me in the right direction and also providing me with a starting point, also for this gives me error messages which helps so the revised is below.

         [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string[] FindAddresses(string prefixText)
    {
        //Check the parameters

        var suburbs = CheckOutService.FindMatchingForPartialAddress(prefixText);
        return suburbs.ToArray();


    }


     $(document).ready(function () {
    $('[ID$=postcode]').live('keyup.autocomplete', function () {

        $(this).autocomplete({
            source: function (request, response) {
                alert(request.term);
                $.ajax({
                    url: '<%=ResolveUrl("AutoCompleteService.asmx/FindAddresses") %>',
                    data: "{ prefixText:'" + request.term + "'}",
                    dataType: "json",
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    success: function (data) {
                        response($.map(data.d, function (item) {
                            return {
                                label: item.split('-')[0],
                                val: item.split('-')[1]
                            }
                        }))
                    },
                    error: function (response) {
                        alert(response.responseText);
                    },
                    failure: function (response) {
                        alert(response.responseText);
                    }
                });
            },
            select: function (e, i) {
            },
            minLength: 1
        });
    });
});

Answers:

Answer

try the following code. that is working for me..

  $(document).ready(function () {
        $('[ID$=txtLastname]').live('keyup.autocomplete', function () {

            $(this).autocomplete({
                source: function (request, response) {
                    $.ajax({
                        url: '<%=ResolveUrl("~/Resources/WebService.asmx/LastName") %>',
                        data: "{ 'prefix': '" + request.term + "'}",
                        dataType: "json",
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        success: function (data) {
                            response($.map(data.d, function (item) {
                                return {
                                    label: item.split('-')[0],
                                    val: item.split('-')[1]
                                }
                            }))
                        },
                        error: function (response) {
                            alert(response.responseText);
                        },
                        failure: function (response) {
                            alert(response.responseText);
                        }
                    });
                },
                select: function (e, i) {
                },
                minLength: 1
            });
        });

Web method is

 [WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string[] LastName(string prefix)
{
    List<string> customers = new List<string>();
    using (SqlConnection conn = new SqlConnection())
    {
        string connectionstring = CCMMUtility.GetCacheForWholeApplication();
        conn.ConnectionString = connectionstring;
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandText = "select distinct top(10) Lastname from tblusers where  " +
            "Lastname  like '%'+ @SearchText + '%' order by Lastname";
            cmd.Parameters.AddWithValue("@SearchText", prefix);
            cmd.Connection = conn;
            conn.Open();
            using (SqlDataReader sdr = cmd.ExecuteReader())
            {
                while (sdr.Read())
                {
                    customers.Add(string.Format("{0}", sdr["Lastname"]));
                }
            }
            conn.Close();
        }
        return customers.ToArray();
    }
}
Answer

If the autocomplete is JQueryUI and you wish to provide some custom parameters to the autocomplete then you will need to provide a function to set the source. Have a look here this should help you to define it.

Also as @Shadow-Wizzard said you may want to make sure that you have the right id's

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.