在asp.net中使用text&image自动完成文本框

时间:2022-01-22 05:11:59

I got this code from aspsnippet.com . Autocomplete textbox with text & image not working. Script is not getting called. Once js function gets called then I can see where is the error but function is not called. Following is code..

我从aspsnippet.com获得了这段代码。自动填充文本框,文字和图片无效。脚本没有被调用。一旦js函数被调用,那么我可以看到错误在哪里,但没有调用函数。以下是代码..

aspx

<form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server"
            EnablePageMethods="true">
        </asp:ScriptManager>
        <div>
            <asp:TextBox ID="txtContactsSearch" runat="server"></asp:TextBox>
            <cc1:AutoCompleteExtender ServiceMethod="SearchEmployees"
                MinimumPrefixLength="1"
                CompletionInterval="100" EnableCaching="false" CompletionSetCount="10"
                TargetControlID="txtContactsSearch" OnClientPopulated="Employees_Populated"
                OnClientItemSelected=" OnEmployeeSelected"
                ID="AutoCompleteExtender1" runat="server" FirstRowSelected="false">
            </cc1:AutoCompleteExtender>
        </div>

        <script type="text/javascript">
            function Employees_Populated(sender, e) {
                alert(Check2)
                var employees = sender.get_completionList().childNodes;
                for (var i = 0; i < employees.length; i++) {
                    var div = document.createElement("DIV");
                    div.innerHTML = "<img style = 'height:50px;width:50px' src = 'list-business/assets/images/doctors/"
                    + employees[i]._value + ".jpg' /><br />";
                    employees[i].appendChild(div);
                }
            }
            function OnEmployeeSelected(source, eventArgs) {
                alert(Check1)
                var idx = source._selectIndex;
                var employees = source.get_completionList().childNodes;
                var value = employees[idx]._value;
                var text = employees[idx].firstChild.nodeValue;
                source.get_element().value = text;
            }
        </script>
    </form>

VB

Imports MySql.Data.MySqlClient
Imports System.Collections.Generic
Imports System.IO

Partial Class _Default
    Inherits System.Web.UI.Page
    <System.Web.Script.Services.ScriptMethod(),
    System.Web.Services.WebMethod()>
    Public Shared Function SearchEmployees(ByVal prefixText As String, ByVal count As Integer) As List(Of String)
        Dim conn As MySqlConnection = New MySqlConnection
        conn.ConnectionString = ConfigurationManager _
         .ConnectionStrings("conio").ConnectionString
        Dim cmd As MySqlCommand = New MySqlCommand
        cmd.CommandText = "select doctorId, name, LastName from " &
        " doctors where Name like @SearchText + '%'"
        cmd.Parameters.AddWithValue("@SearchText", prefixText)
        cmd.Connection = conn
        conn.Open()
        Dim employees As List(Of String) = New List(Of String)
        Dim sdr As MySqlDataReader = cmd.ExecuteReader
        While sdr.Read
            employees.Add(AjaxControlToolkit.AutoCompleteExtender _
            .CreateAutoCompleteItem(String.Format("{0} {1}",
             sdr("Name"), sdr("LastName")), sdr("doctorId").ToString()))
        End While
        conn.Close()
        Return employees
    End Function
End Class

1 个解决方案

#1


1  

Try below,

Imports MySql.Data.MySqlClient
Imports System.Collections.Generic
Imports System.IO

Partial Class _Default
    Inherits System.Web.UI.Page
    <System.Web.Script.Services.ScriptMethod(),
    System.Web.Services.WebMethod()>
    Public Shared Function SearchEmployees(ByVal prefixText As String, ByVal count As Integer) As List(Of String)
        Dim conn As MySqlConnection = New MySqlConnection
        conn.ConnectionString = ConfigurationManager _
         .ConnectionStrings("conio").ConnectionString
        Dim cmd As MySqlCommand = New MySqlCommand
        cmd.CommandText = "select doctorId, name, LastName from " &
        " doctors where Name like '" + prefixText + "%'"            
        cmd.Connection = conn
        conn.Open()
        Dim employees As List(Of String) = New List(Of String)
        Dim sdr As MySqlDataReader = cmd.ExecuteReader
        While sdr.Read
            employees.Add(AjaxControlToolkit.AutoCompleteExtender _
            .CreateAutoCompleteItem(String.Format("{0} {1}",
             sdr("Name"), sdr("LastName")), sdr("doctorId").ToString()))
        End While
        conn.Close()
        Return employees
    End Function
End Class

#1


1  

Try below,

Imports MySql.Data.MySqlClient
Imports System.Collections.Generic
Imports System.IO

Partial Class _Default
    Inherits System.Web.UI.Page
    <System.Web.Script.Services.ScriptMethod(),
    System.Web.Services.WebMethod()>
    Public Shared Function SearchEmployees(ByVal prefixText As String, ByVal count As Integer) As List(Of String)
        Dim conn As MySqlConnection = New MySqlConnection
        conn.ConnectionString = ConfigurationManager _
         .ConnectionStrings("conio").ConnectionString
        Dim cmd As MySqlCommand = New MySqlCommand
        cmd.CommandText = "select doctorId, name, LastName from " &
        " doctors where Name like '" + prefixText + "%'"            
        cmd.Connection = conn
        conn.Open()
        Dim employees As List(Of String) = New List(Of String)
        Dim sdr As MySqlDataReader = cmd.ExecuteReader
        While sdr.Read
            employees.Add(AjaxControlToolkit.AutoCompleteExtender _
            .CreateAutoCompleteItem(String.Format("{0} {1}",
             sdr("Name"), sdr("LastName")), sdr("doctorId").ToString()))
        End While
        conn.Close()
        Return employees
    End Function
End Class