小弟因為目前沒有去申請網路空間上傳檔案所以無法直接把要使用的WS_AutoComplete.asmx檔上傳給大家下載來改所以大家只有自已用下面的自已做一個在去用。
請先去下載ASP.NET的AjaxControlToolkit程式使用AJAX才能使用下面程式。
要開的頁面要是AJAX,要不然就是拉一個ScriptManager
先做一個WS_AutoComplete.asmx
<%@ WebService Language="VB" CodeBehind="WS_AutoComplete.vb" Class="WS_AutoComplete" %>
WS_AutoComplete.vb檔裡面的程式。
Option Strict On
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Web.Configuration
Imports System.Data.SqlClient
Imports System.Collections.Generic
' 若要允許使用 ASP.NET AJAX 從指令碼呼叫此 Web 服務,請取消註解下一行。
<System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class WS_AutoComplete
Inherits System.Web.Services.WebService
<WebMethod(Description:="此方法傳回資料")> _
Public Function GetNameSuggestion(ByVal prefixText As String, _
ByVal count As Integer) As String()
' 定義一個泛用集合物件 List 來儲存建議詞。
Dim suggestions As List(Of String) = New List(Of String)
Try
' 取得資料庫連線字串設定,來建立 SQL 連線物件。
Using cn As New SqlConnection(WebConfigurationManager. _
ConnectionStrings("WEB.CONFIG中的連結字串").ConnectionString)
Dim cmdLiming As SqlCommand = cn.CreateCommand()
' 指派查詢陳述式。
cmdLiming.CommandText = "select distinct log_ip from log where log_ip like @prefixName order by 1"
' 設定參數值。
cmdLiming.Parameters.AddWithValue("@prefixName", _
prefixText & "%")
' 開啟資料庫連接並將資料讀入資料讀取器中
cn.Open()
Using dr As SqlDataReader = cmdLiming.ExecuteReader
Dim matchCount As Integer = 1
Do While (dr.Read AndAlso matchCount <= count)
suggestions.Add(dr.GetSqlString(0).Value)
matchCount += 1
Loop
End Using
End Using
Return suggestions.ToArray()
Catch ex As Exception
suggestions.Add(ex.Message)
Return suggestions.ToArray()
End Try
End Function
End Class
之後只要在頁面上加下程式
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<AjaxControlToolkit:AutoCompleteExtender ID="aceEditName" runat="server"
TargetControlID="TextBox1"
ServiceMethod="GetNameSuggestion"
ServicePath="WS_AutoComplete.asmx"
MinimumPrefixLength="1"
EnableCaching="true"
CompletionSetCount="12"
CompletionInterval="1000" />
</AjaxControlToolkit:AutoCompleteExtender>
這樣就可以了。