自動完成實現方法(ajax,jqueyr,webserver)

來源:互聯網
上載者:User

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.