AJAX 实现自动完成功能

 

自动功能类似GOOGLE。如下图:

                                                

 

需要添加的控件有:

                                              

 

ScriptManager控件,AutoCompleteExtender控件。

添加Web Service。

[WebService(Namespace = "http://tempuri.org/")] 



[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 



[System.Web.Script.Services.ScriptService()] 



public class AutoCompleteService : System.Web.Services.WebService 



{



    public AutoCompleteService ()



 {        



 //InitializeComponent();     



}



 private static string[] m_autoCompleteWordList = null; 

 [WebMethod]  

public String[] GetWordList(string prefixText, int count) 

 {   if(m_autoCompleteWordList == null)  

 {    string[] temp = File.ReadAllLines(Server.MapPath("~/TextFile/SuggestWords.txt"));   

 Array.Sort(temp,new CaseInsensitiveComparer());  

  m_autoCompleteWordList = temp;   }



  int index = Array.BinarySearch(m_autoCompleteWordList, prefixText,           new CaseInsensitiveComparer());  

 if (index < 0)   {    index = ~index;   }



  int matchingCount;  

 for (matchingCount = 0; matchingCount < count &&    index + matchingCount < m_autoCompleteWordList.Length;   matchingCount++)  

 {    

if(!m_autoCompleteWordList[index+matchingCount].StartsWith(prefixText,StringComparison.CurrentCultureIgnoreCase))    

 break;   }



  String [] returnValue = new string[matchingCount];  

 if (matchingCount > 0)  

 {    Array.Copy(m_autoCompleteWordList,index,returnValue,0,matchingCount);   }



  return returnValue; 

 }



} 



在设计页面中设置Autocompleteextender控件:

<cc1:autocompleteextender id="AutoCompleteExtender1" runat="server" targetcontrolid="suggestedInput"   ServicePath="WebServer/AutoCompleteService.asmx" ServiceMethod="GetWordList"></cc1:autocompleteextender>

运行结果:

                                                      

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值