1.AJAX實現方法,如下:
網頁中:
<asp:ScriptManager ID="ScriptManager1" runat="server"/><cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" ServiceMethod="CusName" ServicePath="AutoService.asmx"TargetControlID="customer" MinimumPrefixLength="1" EnableCaching="true" ></cc1:AutoCompleteExtender>
webserver:
[WebMethod] public string[] CusName(string prefixText) { WebClass.List tmp = new WebClass.List(); DbDataReader dr = tmp.ShowName(prefixText); List<string> suggestions = new List<string>();//聲明一泛型集合 while (dr.Read()) { suggestions.Add(dr.GetString(0)); } dr.Close(); return suggestions.ToArray(); }
2.JQUER實現方法,如下:
需要引入的項目:
<link href="css/jquery.autocomplete.css" rel="stylesheet" type="text/css" /> <script language="JavaScript" type="text/javascript" src="js/jquery.js"></script> <script language="JavaScript" type="text/javascript" src="js/jquery.autocomplete.js"></script>
jvascript:
$(document).ready(function(){ $("#contactperson").autocomplete("AutoHandler.ashx",{ delay:10, minChars:2, matchSubset:1, cacheLength:1, onItemSelect:selectItem, onFindValue:findValue, autoFill:true, maxItemsToShow:20 }); });
ASHX:
public void ProcessRequest(HttpContext context) { //context.Response.ContentType = "text/plain"; //context.Response.Write("Hello World"); string prefixText = context.Request.QueryString["q"]; WebClass.RfsList tmp = new WebClass.RfsList(); DbDataReader dr = tmp.ShowCusName(prefixText); StringBuilder items = new StringBuilder(); while (dr.Read()) { items.Append(dr.GetString(0) + "\n"); } dr.Close(); context.Response.Write(items.ToString()); context.Response.End(); }
ASPX網頁同理,css如下:
.ac_results {padding: 0px;border: 1px solid WindowFrame;background-color: Window;overflow: hidden;}.ac_results ul {width: 100%;list-style-position: outside;list-style: none;padding: 0;margin: 0;}.ac_results iframe {display:none;/*sorry for IE5*/display/**/:block;/*sorry for IE5*/position:absolute;top:0;left:0;z-index:-1;filter:mask();width:3000px;height:3000px;}.ac_results li {margin: 0px;padding: 2px 5px;cursor: pointer;display: block;width: 100%;font: menu;font-size: 12px;overflow: hidden;}.ac_loading {background : Window url('../img/onload.gif') right center no-repeat;}.ac_over {background-color: Highlight;color: HighlightText;}
jquery及jquery autocomplete就不貼了。
/Files/cnaspnet/jqueryautocomplete.rar