Sunday, September 23, 2012

Ajax AutoCompleteExtender Page Method Magic

Using Page Methods with AJAX Auto Complete Extenders

    Using AJAX AutocompleteExtender Page Methods can be a tricky proposal, as I have discovered recently while working on a project. There seems to be a few key settings that seem to be elusive. Countless searches later I have put it all together and though I would share it with with you.
 
      Why use page methods instead of a Webservice file ASMX?? If you only have one extender and/or you want to keep everything in one file, using page methods is a quick way to do it.

Here's the quick checklist:


  • Script Manager must have it's EnablePageMethods property set to true
  • Page method must be marked Public and Shared
  • Method must be in the aspx file and not in a user control
  • Method must be marked with   <webmethod> and <Scriptmethod attribute>
  • Method must be declared specifically as outlined below

<WebMethod()> _
    <Script.Services.ScriptMethod()> _
    Public Shared Function GetCompletionList(ByVal prefixText As StringByVal count As IntegerAs System.String()
        Return {""""}
    End Function

The part that stumped me for so long is the fact you can't use the Auto Complete Extender AND use the page methods in a User Control. Dino Esposito touches on this in this MSDN Magazine Article

"A callable page method is a public static (or Shared in Visual Basic® .NET) method defined in the codebehind class and decorated with the same WebMethod attribute used for Web service methods. At present, this is limited to ASPX pages-both inline and codebehind code-but might be extended in the future to user controls and custom controls."

Mr. Esposito's article goes in depth on the usage and options available

Hope this helps someone...  Happy Coding

No comments:

Post a Comment