简体   繁体   English

为什么AutoCompleteExtender触发Page_Load事件而不是service方法?

[英]Why is the AutoCompleteExtender firing the Page_Load event instead of the service method?

So I'm trying to implement the AutoCompleteExtender tool from the AJAX Control Toolkit. 因此,我正在尝试从AJAX控制工具包中实现AutoCompleteExtender工具。

The following is the implementation of the AutoCompleteExtender on my ASPX page: 以下是我的ASPX页面上AutoCompleteExtender的实现:

<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. 我面临的问题是,每当我在文本框中键入字符时,就会触发Page_Load事件而不是GetAllCustomerNames方法。 Could someone please help me find where I'm going wrong? 有人可以帮我找到我要去的地方吗?

Additional info: 附加信息:

  • I'm using Visual Studio 2013. 我正在使用Visual Studio 2013。
  • This is a ASP.NET Web Form application running on .NET 4.5. 这是在.NET 4.5上运行的ASP.NET Web窗体应用程序。
  • 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. 在主文件中指定了ToolkitScriptManager ,并且我已将EnablePageMethods属性设置为true。

Try [System.Web.Services.WebMethod] not WebMethod() and remove the next line. 尝试使用[System.Web.Services.WebMethod]而不是WebMethod()并删除下一行。

You set AutoPostBack="true" on the TextBox, just delete AutoPostBack or set it to false . 您在TextBox上设置AutoPostBack="true" ,只需删除AutoPostBack或将其设置为false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM