【问题标题】:Why is the AutoCompleteExtender firing the Page_Load event instead of the service method?为什么 AutoCompleteExtender 触发 Page_Load 事件而不是服务方法?
【发布时间】:2014-04-22 09:51:14
【问题描述】:

所以我正在尝试从 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。

【问题讨论】:

  • 对于否决这个问题的人,您能解释一下原因吗?

标签: c# asp.net ajaxcontroltoolkit autocompleteextender


【解决方案1】:

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

你在TextBox上设置AutoPostBack="true",删除AutoPostBack或者设置为false即可。

【讨论】:

  • 感谢您指出这一点,但遗憾的是删除它不起作用。我通过删除 AutoPostBack 属性更新了我的原始帖子。
  • 另外,当我在寻找解决方案时,许多人说 ScriptMethod 属性是必要的。
  • 可能对 TextBox 的 AutoComplete="off" 有一些干扰。尝试删除它以及RequiredFieldValidator,以排除这种情况。
  • 还要确保在您的 WebMethod 中没有抛出异常。首先,只需返回一个硬编码的虚拟字符串数组。
  • 不幸的是,删除 AutoComplete 和 RequiredFieldValidator 并没有什么不同。关于 WebMethod,我遇到的问题是该方法没有被触发,所以我无法检查是否从内部触发了任何异常。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-16
  • 2011-12-03
相关资源
最近更新 更多