這控制項需要寫一個webService的方法,供這個控制項調用!
頁面代碼:
<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"//要出現提示的控制項
ServicePath = "WebService.asmx"//要調用的服務的路徑(和頁面在同一目錄下)
ServiceMethod ="GetHostSearchByKeyWords"; //要調用服務的方法
MinimumPrefixLength ="1"//至少輸入幾個字,會出現提示
EnableCaching ="true"//是否啟用緩衝
CompletionSetCount ="2" //出現幾行提示
></cc1:autocompleteextender>
</div>
</form>
選中你的網站--建立WebService.asmx--在新加的檔案中添加如特性
[System.Web.Script.Services.ScriptService()]
public class WebService : System.Web.Services.WebService
{
public WebService()
{
//如果使用設計的組件,請取消注釋以下行
//InitializeComponent();
}
[WebMethod]
//這個方法除了名字可以改之外,其餘都固定不變
public string[] GetHostSearchByKeyWords(string prefixText,int count)
{
return ZhanNeiService .GetHostSearchByKeyWords (prefixText ,count);
}
}
上面方法中調用的方法如下(ZhanNeiService類中的)
public static string[] GetHostSearchByKeyWords(string keySeach, int count)
{
List<ZhanNei> li = new List<ZhanNei>();
string sql = "select top "+count+" * from zhannei where neirong like '"+keySeach+"%' order by neirong desc" ;
SqlDataReader dr = DBHelper.GetReader(sql);
while (dr.Read ())
{
ZhanNei zhanNei = new ZhanNei();
zhanNei.Id = Convert.ToInt32(dr["id"]);
zhanNei.Neirong = dr["neirong"].ToString();
zhanNei.Time = Convert.ToDateTime(dr["time"]);
li.Add(zhanNei);
}
List<string> result = new List<string>();
foreach (ZhanNei var in li )
{
result.Add(var .Neirong);
}
return result.ToArray();
}
運行即可!