未使用自动扩展器调用自动完成方法



我有Ajax AutoExtender控件与文本框相关联。建议的数据是从数据库中选择的。但是我的自动完成代码甚至没有被触发。代码可能有什么问题?谢谢。

<asp:TextBox ID="txtSports" runat="server" CssClass="textbox" placeholder="Sport"></asp:TextBox> 
<ajaxToolkit:AutoCompleteExtender ID="AutoCompleteExtender1" MinimumPrefixLength="1" ServicePath="~/AutoComplete.asmx" CompletionSetCount="20" CompletionInterval="0000" EnableCaching="true" UseContextKey="True" ServiceMethod="GetCompletionList" TargetControlID="txtSports" runat="server"></ajaxToolkit:AutoCompleteExtender> 

自动完成.asmx 文件:

Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.Data.SqlClient
Imports AjaxControlToolkit
' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
<System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class AutoComplete
    Inherits System.Web.Services.WebService
    <System.Web.Services.WebMethodAttribute, System.Web.Script.Services.ScriptMethodAttribute> _
    Public Shared Function GetCompletionList(prefixText As String, count As Integer, contextKey As String) As String()
        Dim listString As New List(Of String)()
        Dim connStr As String = System.Configuration.ConfigurationManager.ConnectionStrings("SportovniPlanetaCS").ConnectionString.ToString()
        Using conn As New SqlConnection(connStr)
            Dim cmd As New SqlCommand((Convert.ToString("SELECT Sport, SportId FROM Sports WHERE  Sport LIKE @Sport")))
            cmd.Parameters.AddWithValue("@Sport", prefixText)
            conn.Open()
            Dim dr As SqlDataReader = cmd.ExecuteReader()
            If dr.HasRows Then
                While dr.Read()
                    listString.Add(AutoCompleteExtender.CreateAutoCompleteItem(dr("Sport").ToString(), dr("SportId").ToString()))
                End While
            End If
        End Using
        Dim str As String() = listString.ToArray()
        Return str
    End Function
End Class

好吧,我认为您不需要为此使用单独的WebService。 您可以创建一个 Web 方法并使其工作。

你可以在这里看到例子...

自动完成演示

在 AjaxToolKit 中添加

OnClientItemSelected="ClientItemSelected"


添加 JavaScript


函数 ClientItemSelected(source, e){

source.get_element().value = (document.all) ? e._item.innerText : e._item.textContent;

var str = $('#<%=txtsports.ClientID %>').val();

}

最新更新