Friday 5 October 2007

Microsoft AJAX Toolkit - using the AutoCompleteExtender without a web service

The AJAX Control Toolkit sample site instructions (http://www.asp.net/ajax/ajaxcontroltoolkit/samples/) do not make it clear that you can use the AutoCompleteExtender without a web service. This is possible without any additional coding. Here's the trick:
  1. Remove the ServicePath from the extender attributes altogether.
  2. Specify the name of your method in the ServiceMethod attribute,
  3. Add your method call to the script of the page itself. It will not work with a codebehind directly. For example:

Put this code in the page markup:
<script runat="server">

[System.Web.Services.WebMethod]

[System.Web.Script.Services.ScriptMethod]

public static string[] GetCompletionList(string prefixText, int count)

{

return "this is sample text".Split()

}

</script>

Also make sure you set the service method to the function name and omit the ServicePath :

<cc1:autocompleteextender id="AutoCompleteExtender1" runat="server" behaviorid="AutoCompleteEx" targetcontrolid="txtOrderNumber" ServiceMethod="GetCompletionList" CompletionInterval="500" EnableCaching="true" CompletionSetCount="20" DelimiterCharacters=";, :">

6 comments:

David said...

I just SignUp to Thank you.
Short article but very clear.
a goog add for Ajax/Atlas web site.

Anonymous said...

I tried this, but it didn't work. I get no errors, but neither a success.
When I type in the textbox, nothing happens...
It doesn't jump into the script method

Do I have some mor to look for what you didn't mention in your article?

Unknown said...

In IE7 its working fine but in IE6 its going back to list controls can any one help me this

Raja said...

Raja

Enable the PageMethods in ScriptManager and try,

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"/>

Cheers

Anonymous said...

Thankyou very much...really helpful

Charles said...

Great article it was just what I needed to know!