Search code examples
asp.netvb.netautocompleteajaxcontroltoolkitwebmethod

Using the AutoCompleteExtender with ASP.Net


How is everyone today?

The Problem
Basically, I'm delving into the world of the AjaxControlToolkit today, with the main aim of fulfilling my AutoComplete requirements. I've set everything up as tutorialised and am a little confused as to why things aren't working (well I have an inkling as to what may be holding me back).

I've added the AjaxControlToolkit dll to my project and in my Markup I have the following :

at the top

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxControlToolkit" %>

then within my content

<ajaxControlToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"></ajaxControlToolkit:ToolkitScriptManager>

<asp:TextBox ID="txtSearch" runat="server" CssClass="search"></asp:TextBox>

<ajaxControlToolkit:AutoCompleteExtender ID="autoCompleteSearchExtender" runat="server" TargetControlID="txtSearch" ServiceMethod="GetCompletionList"></ajaxControlToolkit:AutoCompleteExtender>

Then in the code behind, I have my nice little function (which the breakpoint within is never reached)

<System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()> _
    Public Function GetCompletionList(ByVal prefixText As String, ByVal count As Integer, ByVal contextKey As String) As String()
        ' Get current list
        Dim myList As List(Of MyClass) = GetSearchResultList()

        Return (From s In myList Select s.Name).ToArray()
    End Function

The function is never being called, for a reason I am unsure of.

Potential Issues
There are a couple of potential issues where things may be going wrong:

  • I've tried adding the AjaxControlToolkit dll to the Toolbar in VS (2010), but all the controls are greyed out...
  • The GetCompletionList function (WebMethod) I have written is in the code behind the page. Could this not be called because it has to be in a Web Service perhaps?
  • I've also just realised that my function in the code behind isn't Shared, is this required? Because the list associated with the auto complete is dynamic.

Any help would be appreciated.

Thanks in advance.


Solution

  • Try putting your GetCompletionList function in a web-service (asmx is easiest). Make sure the the web-service class has a [ScriptService] attribute and that your function has a [ScriptMethod] attribute.

    You'll also need to supply the path to the web-service in your AutoCompleteExtender's "ServicePath" property (i.e. ServicePath="~/MyService.asmx")

    Also, you don't need the "contextKey" parameter in your function unless you are passing a context key from your AutoCompleteExtender control.

    hth