ajaxToolkit:AutoCompleteExtender 使用键值对
How to : Use a Key Value Pair in your AutoCompleteExtender ( updated again... ) 









Hi all,
This has come up time and again on the asp.net Ajax forums and has become the Top Voted issue for the AutoCompleteExtender Work Items . I had some time the other day and set upon to write a fix for this .
How do you use it?
1) Attach a handler to the itemSelected Event using the OnClientItemSelected property in the ACE markup
<ajaxToolkit:AutoCompleteExtender runat="server" BehaviorID="AutoCompleteEx" ID="autoComplete1" TargetControlID="myTextBox" ServicePath="~/Services/AutoComplete.asmx" ServiceMethod="GetCompletionListKeyValuePair" ...... OnClientItemSelected ="IAmSelected" > </ajaxToolkit:AutoCompleteExtender>
function IAmSelected( source, eventArgs ) { alert( " Key : "+ eventArgs.get_text() +" Value : "+eventArgs.get_value()); }
2) The Server-Side Method :
The Server-Side Method will return an array of strings as before ,
You create KeyValue Pairs by calling the method :
AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(<key>,<value>);
EX:
[WebMethod] public string[] GetCompletionList(string prefixText, int count) { if (count == 0) { count = 10; } if (prefixText.Equals("xyz")) { return new string[0]; } Random random = new Random(); List<string> items = new List<string>(count); for (int i = 0; i < count; i++) { char c1 = (char)random.Next(65, 90); char c2 = (char)random.Next(97, 122); char c3 = (char)random.Next(97, 122); items.Add(AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(prefixText + c1 + c2 + c3, i.ToString())); } return items.ToArray(); }
The Display of the ACE does not change , it still remains the same :
Once you select an item from the DropDown of the ACE , you get an alert which shows you the value of the selectedItem.
In Your applications , instead of the Alert , process some business logic as per your requirements.
Hope this is an useful Addition.
Download the Latest AjaxControlToolkit from :
http://www.codeplex.com/AtlasControlToolkit/Release/ProjectReleases.aspx?ReleaseId=4941
[Update ]
You don't need to specify the UseKeyValuepairs attibute anymore ( in fact , its been removed :) ).
The ACE script is intelligent enough to realize when Key/Value pairs are returned automatically.
And also , the method CreateKeyvaluePair has been changed to CreateAutoCompleteItem.
Filed under: JavaScript, AutoCompleteExtender
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
http://moosdau.blog.163.com/blog/static/437112820088195216888/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY