您的位置:首页 > 其它

ajaxToolkit:AutoCompleteExtender 使用键值对

2011-05-22 08:24 381 查看
很奇怪为什么MS不把autocomplete封装成可以直接服务器端使用键值对,而要使用者自己定义客户端事件解决。。。。下面是网上看到的使用键值对方法,有空封装一下,嘿嘿

Howto:UseaKeyValuePairinyourAutoCompleteExtender(updatedagain...)



















Hiall,

Thishascomeuptimeandagainontheasp.netAjaxforumsandhasbecometheTopVotedissuefortheAutoCompleteExtenderWorkItems.Ihadsometimetheotherdayandsetupontowriteafixforthis.

Howdoyouuseit?

1)AttachahandlertotheitemSelectedEventusingtheOnClientItemSelectedpropertyintheACEmarkup

<ajaxToolkit:AutoCompleteExtenderrunat="server"
BehaviorID="AutoCompleteEx"ID="autoComplete1"
TargetControlID="myTextBox"ServicePath="~/Services/AutoComplete.asmx"
ServiceMethod="GetCompletionListKeyValuePair"
......
OnClientItemSelected="IAmSelected"
>
</ajaxToolkit:AutoCompleteExtender>

functionIAmSelected(source,eventArgs){
alert("Key:"+eventArgs.get_text()+"Value:"+eventArgs.get_value());
}

2)TheServer-SideMethod:

TheServer-SideMethodwillreturnanarrayofstringsasbefore,

YoucreateKeyValuePairsbycallingthemethod:

AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(<key>,<value>);

EX:

[WebMethod]

publicstring[]GetCompletionList(stringprefixText,intcount)
{

if(count==0)
{

count=10;

}

if(prefixText.Equals("xyz"))
{

returnnewstring[0];

}

Randomrandom=newRandom();

List<string>items=newList<string>(count);

for(inti=0;i<count;i++)
{

charc1=(char)random.Next(65,90);

charc2=(char)random.Next(97,122);

charc3=(char)random.Next(97,122);

items.Add(AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(prefixText+c1+c2+c3,i.ToString()));

}

returnitems.ToArray();

}

TheDisplayoftheACEdoesnotchange,itstillremainsthesame:

OnceyouselectanitemfromtheDropDownoftheACE,yougetanalertwhichshowsyouthevalueoftheselectedItem.

InYourapplications,insteadoftheAlert,processsomebusinesslogicasperyourrequirements.

HopethisisanusefulAddition.


DownloadtheLatestAjaxControlToolkitfrom:

http://www.codeplex.com/AtlasControlToolkit/Release/ProjectReleases.aspx?ReleaseId=4941

[Update]

Youdon'tneedtospecifytheUseKeyValuepairsattibuteanymore(infact,itsbeenremoved:)).

TheACEscriptisintelligentenoughtorealizewhenKey/Valuepairsarereturnedautomatically.

Andalso,themethodCreateKeyvaluePairhasbeenchangedtoCreateAutoCompleteItem.

Filedunder:JavaScript,AutoCompleteExtender

http://blogs.msdn.com/phaniraj/archive/2007/06/19/how-to-use-a-key-value-pair-in-your-autocompleteextender.aspx

http://spicydotnet.spaces.live.com/blog/cns!3810EC86F275BE32!174.entry?wa=wsignin1.0&sa=720268041

http://forums.asp.net/t/1162005.aspx

http://www.codeplex.com/AjaxControlToolkit/WorkItem/View.aspx?WorkItemId=9043

http://www.codeplex.com/AjaxControlToolkit/WorkItem/View.aspx?WorkItemId=12910
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: