In AutoCompleteExtender in almost every key press in the textbox, the Ajax call goes to the associated web service. Few service calls are stopped by caching if it is enabled. But in most cases it fires. So, if you enter 20 character, then 20 times call goes to the service and subsequently, 20 times request goes to the database from the service and so on.
If you want to do a more intelligent filtration of the service call, then there is no direct way to do that.
Example cases:
– in your textbox, after certain number of characters you do not need the auto complete suggestion any more.
– you want auto complete suggestion only after a particular pattern of character sequence (e.g. for.., @..) in the text box.
The indirect way to do it is, hook into the textbox – onkeydown event, and remove the service method using javascript.
|
In this sample code below, I used a TextBox (txtDemo) with AutocompleteExtender.
The web service EmployeeName.asmx has a method GetEmployeeName()that support the extender to fill in the suggestion.
AutocompleteExtender’s BehaviorID is AutoCompleteExtender.
The following code blocks the AutoCompleteExtender service call, after 10 characters in the text box. If you have any other requirement, then you can write your own logic in the javascript.
|
-------------------------------------------------------------------------------- Java script: function filterAutocompleteCall() { var txtDemo = document.getElementById('<%=txtDemo.ClientID %>'); var autoCompleteExtender = $find("AutoCompleteExtender"); if (txtDemo.length > 10) { autoCompleteExtender.set_serviceMethod(" "); } else { autoCompleteExtender.set_serviceMethod("GetEmployeeName"); } } -------------------------------------------------------------------------------- In .aspx Page: <cc:AutoCompleteExtender ID="txtDemo_AutoCompleteExtender" runat="server" TargetControlID="txtDemo" Enabled="True" EnableCaching="true" MinimumPrefixLength="3" FirstRowSelected="True" BehaviorID="AutoCompleteExtender" > </cc:AutoCompleteExtender> <asp:TextBox ID="txtDemo" runat="server" ></asp:TextBox> -------------------------------------------------------------------------------- In code behind: protected void Page_Load(object sender, EventArgs e) { txtDemo_AutoCompleteExtender.ServiceMethod = "GetEmployeeName"; txtDemo_AutoCompleteExtender.ServicePath = "~/GPSWebService.asmx"; txtDemo.Attributes.Add("onkeydown", "filterAutocompleteCall()"); }