nableCaching="false"
FirstRowSelected="true"
UseContextKey="True"
MinimumPrefixLength="0"
CompletionInterval="100"
ServicePath="WebService.asmx"
ServiceMethod="GetEnglishName">
</asp:AutoCompleteExtender>
</div>
</form>
</body>
</html>
aspx.cs
复制代码 代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class TestSearch : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Attributes.Add("onkeydown", "return OnTxtPersonInfoKeyDown();");
TextBox2.Attributes.Add("onkeydown", "return OnTxtPersonInfoKeyDown2();");
}
}
webservice.asmx.cs
复制代码 代码如下:
<%@ WebService Language="C#" Class="WebService" %>
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{
//从数据库中读取匹配信息
[WebMethod]
[ScriptMethod]
public string[] GetEnglishName(string prefixText, int count, string contextKey)
{
SQLHelper sqlH = new SQLHelper();
//contextKey = "t";
string strSql = " select [EnglishName] from [Employee] where [LeftDate] is null and [EnglishName] like ''" + contextKey + "%'' order by [EnglishName] ";
DataTable dt = sqlH.ExecuteQuery(strSql, CommandType.Text);
List<string> suggestions = new List<string>();//声明一泛型集合
suggestions.Clear();
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
suggestions.Add(dt.Rows[i][0].ToString());
}
}
return suggestions.ToArray();
}
}