locked
Why is the AutoCompleteExtender firing the Page_Load event instead of the service method? RRS feed

  • Question

  • User915955336 posted

    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" />
    <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.

    Thanks in advance!

    Edit:

    1. Made the WebMethod static
    Tuesday, April 22, 2014 10:37 AM

Answers

  • User-933407369 posted

    i have a try with the codes below:

    .aspx page:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %>
    
    <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"></asp:ToolkitScriptManager>
        <div>
        <asp:TextBox runat="server" ID="CustomerTextBox" CssClass="form-control" />
    <asp:RequiredFieldValidator runat="server" ControlToValidate="CustomerTextBox"
        CssClass="text-danger" ErrorMessage="The Customer field is required." Display="None" />
    <asp:AutoCompleteExtender ID="CustomerAutoCompleteExtender" runat="server" TargetControlID="CustomerTextBox"
        MinimumPrefixLength="1" EnableCaching="true" CompletionSetCount="20" CompletionInterval="1000" 
        ServiceMethod="GetAllCustomerNames">
    </asp:AutoCompleteExtender>
        </div>
        </form>
    </body>
    </html>
    

    Code Snippet:

    public partial class Default5 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    
        [System.Web.Services.WebMethod]
        [System.Web.Script.Services.ScriptMethod]
        public static string[] GetAllCustomerNames(string prefixText, int count)
        {
            if (count == 0)
            {
                count = 10;
            }
    
            if (prefixText.Equals("xyz"))
            {
                return new string[0];
            }
            List<string> allCustomerNames = new List<string>();
            Random random = new Random();
            for (int i = 0; i < count; i++)
            {
                char c1 = (char)random.Next(65, 90);
                char c2 = (char)random.Next(97, 122);
                char c3 = (char)random.Next(97, 122);
    
                allCustomerNames.Add(prefixText + c1 + c2 + c3);
            }
    
            return allCustomerNames.ToArray();
        }
    }

    please refer to the link : Using AJAX AutoCompleteExtender for autosuggest

    it is working for me, i hope it helps you.

    if it is not helpful for you, i assume that you maybe place the AutoCompleteExtender in usecontrol or have multiple AutoCompleteExtender in your page.

    You'd better  provide  a simple demo,which can easily reproduce the problem?

    Thank you for your understanding.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 23, 2014 2:37 AM

All replies

  • User-1360095595 posted

    Try specifying the ServicePath property of the extender pointing it to your web service. 

    Also, your webmethod has to be static. So add that to the signature. 

    Edit: corrected wrong word. 

    Tuesday, April 22, 2014 1:27 PM
  • User915955336 posted

    But I'm not using a web service. The service method is in the code behind file of aspx page (i.e. aspx.cs file). Should I specify that as the ServicePath?

    I tried making the webmethod static but that didn't make a difference either.

    Tuesday, April 22, 2014 1:34 PM
  • User915955336 posted

    chetan.sarode

    Refer this - http://www.asp.net/ajaxlibrary/ajaxcontroltoolkitsamplesite/autocomplete/autocomplete.aspx

    That was my first point of reference but since I can't seem to find what's wrong I thought I'd come here

    chetan.sarode

    http://stackoverflow.com/questions/23216073/why-is-the-autocompleteextender-firing-the-page-load-event-instead-of-the-servic

    I posted that Laughing

    Wednesday, April 23, 2014 12:01 AM
  • User-933407369 posted

    i have a try with the codes below:

    .aspx page:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %>
    
    <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"></asp:ToolkitScriptManager>
        <div>
        <asp:TextBox runat="server" ID="CustomerTextBox" CssClass="form-control" />
    <asp:RequiredFieldValidator runat="server" ControlToValidate="CustomerTextBox"
        CssClass="text-danger" ErrorMessage="The Customer field is required." Display="None" />
    <asp:AutoCompleteExtender ID="CustomerAutoCompleteExtender" runat="server" TargetControlID="CustomerTextBox"
        MinimumPrefixLength="1" EnableCaching="true" CompletionSetCount="20" CompletionInterval="1000" 
        ServiceMethod="GetAllCustomerNames">
    </asp:AutoCompleteExtender>
        </div>
        </form>
    </body>
    </html>
    

    Code Snippet:

    public partial class Default5 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    
        [System.Web.Services.WebMethod]
        [System.Web.Script.Services.ScriptMethod]
        public static string[] GetAllCustomerNames(string prefixText, int count)
        {
            if (count == 0)
            {
                count = 10;
            }
    
            if (prefixText.Equals("xyz"))
            {
                return new string[0];
            }
            List<string> allCustomerNames = new List<string>();
            Random random = new Random();
            for (int i = 0; i < count; i++)
            {
                char c1 = (char)random.Next(65, 90);
                char c2 = (char)random.Next(97, 122);
                char c3 = (char)random.Next(97, 122);
    
                allCustomerNames.Add(prefixText + c1 + c2 + c3);
            }
    
            return allCustomerNames.ToArray();
        }
    }

    please refer to the link : Using AJAX AutoCompleteExtender for autosuggest

    it is working for me, i hope it helps you.

    if it is not helpful for you, i assume that you maybe place the AutoCompleteExtender in usecontrol or have multiple AutoCompleteExtender in your page.

    You'd better  provide  a simple demo,which can easily reproduce the problem?

    Thank you for your understanding.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 23, 2014 2:37 AM