首先在http://ajaxcontroltoolkit.codeplex.com/页面,然后,进入 download 下载页面 ,下载AjaxControlToolkit.Binary.NET4.zip 文件。解压缩后,里面有一个 AjaxControlToolkit.dll 文件,版本号是 4.1
然后,复制此文件,到你项目的 bin 目录中。
然后,在你项目中的 "引用" 添加对此文件的引用。
在工具栏中,新建 “AjaxControltoolkit” 选项卡。然后,添加选项,找到你项目中的Bin目录里的 AjaxControlToolkit.dll 文件,把控件全部添加到工具栏中。
在工具栏的 “Ajax Extensions” 中,找到 "ScriptManager" , 并拖到你的 aspx 页面中。
拖 AutoCompleteExtender 控件到你 aspx 页面中的 TextBox1 中,然后,设置此控件如下:
<
asp:TextBox ID
=
"
TextBox1
"
runat
=
"
server
"
AutoPostBack
=
"
True
"
ontextchanged = " TextBox1_TextChanged " ></ asp:TextBox >
< asp:AutoCompleteExtender ID = " TextBox1_AutoCompleteExtender " runat = " server " CompletionSetCount = " 10 " CompletionInterval = " 2 " MinimumPrefixLength = " 1 "
TargetControlID = " TextBox1 " ServicePath = " WebServices/AutoCompleteEmptyTelCode.asmx " ServiceMethod = " GetCompletionList " UseContextKey = " True " ></ asp:AutoCompleteExtender >
ontextchanged = " TextBox1_TextChanged " ></ asp:TextBox >
< asp:AutoCompleteExtender ID = " TextBox1_AutoCompleteExtender " runat = " server " CompletionSetCount = " 10 " CompletionInterval = " 2 " MinimumPrefixLength = " 1 "
TargetControlID = " TextBox1 " ServicePath = " WebServices/AutoCompleteEmptyTelCode.asmx " ServiceMethod = " GetCompletionList " UseContextKey = " True " ></ asp:AutoCompleteExtender >
然后 在你 web 项目中 建 WebServices 目录,然后,建一个新的 webserivice 文件,如 AutoCompleteEmptyTelCode.asmx
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.Services;
using DAL;
namespace WebUI.WebServices
{
/// <summary>
/// AutoCompleteEmptyTelCode 的摘要说明
/// </summary>
[WebService(Namespace = " http://tempuri.org/ " )]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem( false )]
[System.Web.Script.Services.ScriptService]
public class AutoCompleteEmptyTelCode : System.Web.Services.WebService
{
Db newdb = new Db();
[WebMethod]
public string [] GetCompletionList( string prefixText, int count)
{
if (count == 0 )
{
count = 20 ;
}
string sql = " select top 50 电话号码 from TelInfo where (外配线号 is null or 外配线号 = '') and 电话号码 like ' " + prefixText + " %' group by 电话号码 order by 电话号码 desc " ;
DataSet ds = newdb.CommonDataSet(sql);
List < string > items = new List < string > (count);
for ( int i = 0 ; i < ds.Tables[ 0 ].Rows.Count; i ++ )
{
string c1 = ds.Tables[ 0 ].Rows[i][ " 电话号码 " ].ToString();
items.Add(c1);
}
return items.ToArray();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.Services;
using DAL;
namespace WebUI.WebServices
{
/// <summary>
/// AutoCompleteEmptyTelCode 的摘要说明
/// </summary>
[WebService(Namespace = " http://tempuri.org/ " )]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem( false )]
[System.Web.Script.Services.ScriptService]
public class AutoCompleteEmptyTelCode : System.Web.Services.WebService
{
Db newdb = new Db();
[WebMethod]
public string [] GetCompletionList( string prefixText, int count)
{
if (count == 0 )
{
count = 20 ;
}
string sql = " select top 50 电话号码 from TelInfo where (外配线号 is null or 外配线号 = '') and 电话号码 like ' " + prefixText + " %' group by 电话号码 order by 电话号码 desc " ;
DataSet ds = newdb.CommonDataSet(sql);
List < string > items = new List < string > (count);
for ( int i = 0 ; i < ds.Tables[ 0 ].Rows.Count; i ++ )
{
string c1 = ds.Tables[ 0 ].Rows[i][ " 电话号码 " ].ToString();
items.Add(c1);
}
return items.ToArray();
}
}
}
如果,测试时,并没有出现联想下拉列表,则要考虑可能是你的 sql 语句有问题,可以把你的这段 sql 语句放在 sql查询分析器中先试验一下,是否 sql 有问题。