The MS Ajax AutoComplete extender is able to pull its completion list from either a Web Service or a Page Method. All the examples I could find used Web Service, but I wanted to use the Page Method approach.
As I understand it (not very well) the advantages of a page method are that
- The code for the completion list callback is kept with the rest of the code for the page containing the control.
- I can easily access session state in the callback method.
To get the extender to work with page methods requires several modifications to the page.
First, we need to tell the AJAX script manager to allow callbacks to page methods.
Second, we need to set up the AutoComplete extender to call a page method. The main thing here is that there is no ServicePath property.
Finally, we implement the method that returns the completion list in the code-behind.
Public Shared Function GetCompletionList( _
ByVal prefixText As String, _
ByVal count As Integer) _
‘ Insert code to return a string array here…
Notice the signature of this method. It must be shared (static in c#) method. The parameters passed to the method must be called “prefixText” and “count”. Nothing else will work. However, you are permitted to return a List(Of String), instead of an array of strings, if that suits you better.
Of course, you’ll need to import “System.Web.Services” at the top of the code-behind, so you will need to add a reference to “System.Web.Services” to your project if you don’t already have one.
My thanks to Fredrik Normén for his blog entry on Calling a static page method with ASP.Net Ajax.