首先说vs可以安装这套dll,也可以直接找到Ajax.dll和AjaxControlToolkit.dll这两个封装好的引用.
配置web.config
<connectionStrings>
<add name="strConn" connectionString="Data Source=.;User ID=sa;Password=123;Initial Catalog=tb1" />
</connectionStrings>
建立aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="JiBingZhenduan.aspx.cs" Inherits="LinChuang_JiBingZhenduan" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" TargetControlID="TextBox1"
MinimumPrefixLength="1" CompletionSetCount="100" EnableCaching="true" FirstRowSelected="true"
ServicePath="WebService.asmx" ServiceMethod="GetCompleteDepart">
</cc1:AutoCompleteExtender>
</div>
</form>
</body>
</html>
注意最上边的<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
其实ajax大体是差不多的就是需要此代码
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
然后放一个输入框TextBox用于搜索
然后放入一个cc1:AutoCompleteExtender这个ajax自定义控件
重要属性TargetControlID:指定要实现提示功能的控件。
ServicePath:WebService的路径,提取数据的方法是写在一个WebService中的。
ServeiceMethod:写在WebService(我的文件名是 MYWebService)中的用于提取数据的方法的名字。
MinimumPrefixLength:用来设置用户输入多少字母才出现提示效果。
CompletionSetCount:设置提示数据的行数。
CompletionInterval:从服务器获取书的时间间隔,单位是毫秒。
aspx.cs不需要代码
建立WebService.asmx
<%@ WebService Language="C#" CodeBehind="~/App_Code/MYWebService.cs" Class="MYWebService" %>
MYWebService.cs注意对应App_Code内建立MYWebService.cs文件
App_Code内建立MYWebService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web.Services.Protocols;
/// <summary>
///WebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService]
public class MYWebService : System.Web.Services.WebService
{
public MYWebService()
{
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
//定义数组保存获取的内容
private string[] autoCompleteWordList = null;
//两个参数“prefixText”表示用户输入的前缀,count表示返回的个数
[WebMethod]
public String[] GetCompleteDepart(string prefixText, int count)
{
//检测参数是否为空
if (string.IsNullOrEmpty(prefixText) == true || count <= 0)
return null;
// 如果数组为空
if (autoCompleteWordList == null)
{
//读取数据库的内容
string str = ConfigurationManager.ConnectionStrings["strConn"].ConnectionString;
SqlConnection connection = new SqlConnection(str); //生成数据库连接对象
connection.Open();//打开数据库连接
SqlDataAdapter da = new SqlDataAdapter("select a from a1 where a like '" + prefixText + "%'order by a ",connection);
DataSet ds = new DataSet();
da.Fill(ds);
//读取内容文件的数据到临时数组
string[] temp = new string[ds.Tables[0].Rows.Count];
int i = 0;
foreach (DataRow dr in ds.Tables[0].Rows)
{
temp[i] = dr["a"].ToString();
i++;
}
Array.Sort(temp, new CaseInsensitiveComparer());
//将临时数组的内容赋给返回数组
autoCompleteWordList = temp;
if (connection.State == ConnectionState.Open)
connection.Close();
} //定位二叉树搜索的起点
int index = Array.BinarySearch(autoCompleteWordList, prefixText, new CaseInsensitiveComparer());
if (index < 0)
{
//修正起点
index = ~index;
}
//搜索符合条件的数据
int matchCount = 0;
for (matchCount = 0; matchCount < count && matchCount + index < autoCompleteWordList.Length; matchCount++)
{
///查看开头字符串相同的项
if (autoCompleteWordList[index + matchCount].StartsWith(prefixText, StringComparison.CurrentCultureIgnoreCase) == false)
{
break;
}
}
//处理搜索结果
string[] matchResultList = new string[matchCount];
if (matchCount > 0)
{
//复制搜索结果
Array.Copy(autoCompleteWordList, index, matchResultList, 0, matchCount);
}
return matchResultList;
//return autoCompleteWordList;
}
}
在bin文件下添加引用Ajax.dll和AjaxControlToolkit.dll
运行调试.可以模糊匹配,如abcd,可搜索a,ab,abc,abcd均可自动匹配