繁体   English   中英

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

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

因此,我正在尝试从AJAX控制工具包中实现AutoCompleteExtender工具。

以下是我的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>

这是在文件后面的代码中实现的服务方法:

[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();
}

我面临的问题是,每当我在文本框中键入字符时,就会触发Page_Load事件而不是GetAllCustomerNames方法。 有人可以帮我找到我要去的地方吗?

附加信息:

  • 我正在使用Visual Studio 2013。
  • 这是在.NET 4.5上运行的ASP.NET Web窗体应用程序。
  • 创建新项目时,我使用默认样式和模板,因此正在使用母版页。
  • 在主文件中指定了ToolkitScriptManager ,并且我已将EnablePageMethods属性设置为true。

尝试使用[System.Web.Services.WebMethod]而不是WebMethod()并删除下一行。

您在TextBox上设置AutoPostBack="true" ,只需删除AutoPostBack或将其设置为false

暂无
暂无

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

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