关于Ajax控件中的AutoCompleteExtender控件的一个简单应用
关于Ajax控件中的AutoCompleteExtender控件的一个简单应用
今天在看一个朋友用到了这个AutoCompleteExtender控件,感觉不错,给客户的体验应该是不错的,这个控件主要是用于根据客户输入的字符来搜索出相应的内容显示在下拉列表框中。 看这个控件的名字就知道是一个可扩展的控件了,它必须依赖于别人而存在-----这个别人指的是文本框. 所以,首先要有一个宿主文本框:
<asp:TextBoxID="TextBox9"runat="server"></asp:TextBox>
<cc1:AutoCompleteExtenderID="TextBox9_AutoCompleteExtender"runat="server"Enabled="True"
OnClientItemSelected="ProductSelect"EnableCaching="true"CompletionInterval="100"
MinimumPrefixLength="1"ServiceMethod="GetProductList"ServicePath="~/Marketing/MyAutoComplete.asmx"
TargetControlID="TextBox9">
</cc1:AutoCompleteExtender>
这就是一个默认的文本框,不需要任何多余的设定. 然后定义一个AutoCompleteExtender,这里我们只设置了最基本的选项,其它的选项后面我会说明:
usingSystem;
usingSystem.Collections;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.Services;
usingSystem.Web.Services.Protocols;
usingSystem.Xml.Linq;
usingSystem.Collections.Generic;
usingSystem.Data.SqlClient;
///<summary>
///MyAutoComplete
///</summary>
["http://tempuri.org/")]
[WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
publicclassMyAutoComplete: System.Web.Services.WebService{
publicMyAutoComplete () {
[
WebMethod]//这个方法是必须的,每建立一个方法就的加上这个,不然远程客户端就不能调用定义的方法
public
List<string
> GetResult(string
prefixText,int
count)
{
List<string> getrs =newList<string>();
stringsql ="select top "+ count.ToString() +" UserName,Pwd from Test where UserName like '%"+ prefixText.ToString() +"%' ";
SqlDataReaderdr =DB.getdatareader(sql);
while(dr.Read())
{
stringcontent = dr.GetString(0);
getrs.Add(content);
}
dr.Close();
//dr.Dispose();
returngetrs;
}
}
这个函数的第一个参数,就是用户录入的内容,第二个参数,是定义中指定的下拉列表长度,也就是要返回的项的个数,我们在定义中没有指定长度,它会使用一个默认长度10 . 4.AutoCompleteExtender的其它属性 其实还有一个属性是比较重要的,就是OnClientItemSelected,这个是调用一个方法,该方法用于对已经选择项后执行的事件。这个以后有时间再举例说明。 转载于:http://blog.csdn.net/liu_ben_qian/article/details/6649261(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |