Stupid Romeo Stupid Romeo - 7 months ago 24
SQL Question

autocomplete textbox with text & image in asp.net

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..

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

Answer

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
Comments