[英]Implement jQuery AutoComplete TextBox from database using AJAX PageMethods in ASP.Net
[英]autocomplete textbox with text & image in asp.net
我從 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.