mysql - asp.net 中带有文本和图像的自动完成文本框

标签 mysql asp.net vb.net autocomplete

我从 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

最佳答案

试试下面,

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

关于mysql - asp.net 中带有文本和图像的自动完成文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36710994/

相关文章:

c# - Asp.net 核心返回 View 或 Json/XML 的最干净的方法

vb.net - MSBuild:条件构造(项目引用 | 文件引用)

mysql : Any command to use instead of mysql_insert_id(); in PHP?

mysql - 关于自动修复任何失败的表优化的脚本有什么想法吗?

c# - Google Analytics 和主页/用户控制交换

xml - 将更新的 XMLDocument 保存为字符串(vb.net 或其他 .net 语言。)

c# - session 状态跨子域共享 session ,VB.NET 网站 C# MVC 4 应用程序

php - 获得个人资料 View 的总体 View ?

php - 在一个字段中存储多个值

asp.net - 使用 ASP.NET MVC 进行模型绑定(bind)内部嵌套模型