簡體   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