0

So I'm trying to implement the AutoCompleteExtender tool from the AJAX Control Toolkit.

The following is the implementation of the AutoCompleteExtender on my ASPX page:

<asp:TextBox runat="server" ID="CustomerTextBox" CssClass="form-control" AutoComplete="off" />
<asp:RequiredFieldValidator runat="server" ControlToValidate="CustomerTextBox"
    CssClass="text-danger" ErrorMessage="The Customer field is required." Display="None" />
<ajaxToolkit:AutoCompleteExtender ID="CustomerAutoCompleteExtender" runat="server" TargetControlID="CustomerTextBox"
    MinimumPrefixLength="1" EnableCaching="true" CompletionSetCount="1" CompletionInterval="1000" 
    ServiceMethod="GetAllCustomerNames">
</ajaxToolkit:AutoCompleteExtender>

This is the service method implemented in the code behind file:

[System.Web.Services.WebMethod()]
[System.Web.Script.Services.ScriptMethod()]
public static string[] GetAllCustomerNames(string prefixText, int count)
{

    List<string> allCustomerNames = new List<string>();
    List<Customer> allCustomers = GetAllCustomers();

    foreach (Customer customer in allCustomers)
    {
        if (customer.CustomerName.Contains(prefixText))
        {
            allCustomerNames.Add(customer.CustomerName);
        }
    }

    return allCustomerNames.ToArray();
}

The problem I'm facing is that whenever I type a character into the text box the Page_Load event fires instead of the GetAllCustomerNames method. Could someone please help me find where I'm going wrong?

Additional info:

  • I'm using Visual Studio 2013.
  • This is a ASP.NET Web Form application running on .NET 4.5.
  • I used the default style and template as when a new project is created and so a Master Page is being used.
  • The ToolkitScriptManager is specified in the Master File and I have set EnablePageMethods property to true.
Rusty Wizard
  • 531
  • 6
  • 20

1 Answers1

0

Try [System.Web.Services.WebMethod] not WebMethod() and remove the next line.

You set AutoPostBack="true" on the TextBox, just delete AutoPostBack or set it to false.

cudahead
  • 153
  • 6
  • Thanks for pointing that out but sadly removing it didn't work. I've updated my original post by removing the AutoPostBack property. – Rusty Wizard Apr 22 '14 at 10:09
  • Also while I was searching for a solution, many said that the ScriptMethod attribute is necessary. – Rusty Wizard Apr 22 '14 at 10:16
  • Maybe there is some interference with AutoComplete="off" of the TextBox. Try removing it, and also the RequiredFieldValidator, just to rule that out. – cudahead Apr 22 '14 at 10:36
  • Also make sure that no Exception is thrown in your WebMethod. As a start simply return a hard coded dummy String Array. – cudahead Apr 22 '14 at 10:53
  • Removing AutoComplete and the RequiredFieldValidator didn't make a difference unfortunately. Regarding the WebMethod, the issue I'm having is the method is not being fired so I can't check whether is any exception being fired from within. – Rusty Wizard Apr 22 '14 at 11:13