开发者

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"></ajaxControlTool开发者_运维百科kit: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.


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


Update: Add these attributes and try it. I have implemented and it works with page behind web methods.

ServiceMethod="yourGetfunction"
MinimumPrefixLength="2" 
CompletionInterval="100"
EnableCaching="FALSE"
CompletionSetCount="20"


You can check this solution: http://suggester.codeplex.com/

Demo to test: http://show-demos.net/suggester/

Its not from ALAX Toolkit but it uses ASP.NET AJAX and jQuery and has more rich functionality


I kind of made this working:

<WebMethod()> _
    Public Shared Function GetCompletionList(ByVal prefixText As String, ByVal count As Integer) As List(Of String)
        Dim listData As New List(Of String)
        listData.Add("A")
        listData.Add("B")
        listData.Add("C")
        Return listData
    End Function

My html:

<form id="maincontent" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods = "true">
    </asp:ScriptManager>
    <div>
           <table style="margin-top:40px;color:White">  
            <tr>  
                <td>  
                    Type in your search:  
                </td>  
                <td>  
                    <asp:TextBox ID="searchBox" runat="server"></asp:TextBox>  
                    <asp:AutoCompleteExtender ServiceMethod="GetCompletionList" MinimumPrefixLength="1"  
                        CompletionInterval="10" EnableCaching="false" CompletionSetCount="1" TargetControlID="searchBox"  
                        ID="AutoCompleteExtenderPersonSearch" runat="server" FirstRowSelected="false">  
                    </asp:AutoCompleteExtender>  
                </td>
            </tr>  
        </table>  

    </div>
    </form>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜