[英]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方法。 有人可以幫我找到我要去的地方嗎?
附加信息:
嘗試使用[System.Web.Services.WebMethod]而不是WebMethod()並刪除下一行。
您在TextBox上設置AutoPostBack="true"
,只需刪除AutoPostBack或將其設置為false
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.