我从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
答案 0 :(得分:1)
尝试以下,
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