Ajax搜索框的自动提示功能

来源:互联网 发布:vb怎么安装msdn 编辑:程序博客网 时间:2024/05/05 19:22
  1. Html页部分
  2. ————————————————————————————————————————
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml">
  5. <head>
  6.     <script type="text/javascript" src="lib/jquery.js">
  7.     </script>
  8.     <script type='text/javascript' src='lib/jquery.bgiframe.min.js'></script>
  9.     <script type='text/javascript' src='lib/jquery.ajaxQueue.js'></script>
  10.     <script type='text/javascript' src='lib/thickbox-compressed.js'></script>
  11.     <script type='text/javascript' src='lib/jquery.autocomplete.js'></script>
  12.     <script type='text/javascript' src='lib/localdata.js'></script>
  13.     <link rel="stylesheet" type="text/css" href="lib/main.css" />
  14.     <link rel="stylesheet" type="text/css" href="lib/jquery.autocomplete.css" />
  15.     <link rel="stylesheet" type="text/css" href="lib/thickbox.css" />
  16.     <script type="text/javascript">
  17. $().ready(function() {
  18.     
  19.         $("#username").autocomplete("AutoTip.aspx",   
  20.              { delay:10, 
  21.                minChars:1,
  22.                matchSubset:1,
  23.                matchContains:false,   
  24.               cacheLength:10,  
  25.               onItemSelect:selectItem,   
  26.               onFindValue:findValue,      
  27.               formatItem:formatItem,   
  28.               autoFill:false     
  29.               });
  30.     
  31.     })
  32.     function findValue(li) {
  33.     if( li == null ) return alert("No match!");
  34.      // if coming from an AJAX call, let's use the CityId as the value    
  35.      if( !!li.extra ) var sValue = li.extra[0];   
  36.      // otherwise, let's just display the value in the text box
  37.      else var sValue = li.selectValue; 
  38.         //alert("The value you selected was: " + sValue);
  39.         }
  40.     
  41.     function selectItem(li) 
  42.     {    findValue(li);
  43.     }
  44.     
  45.     
  46.     function formatItem(row) 
  47.     {    return row[0];
  48.     
  49.     //return row[0] + " (id: " + row[1] + ")"    //如果有其他参数调用row[1],对应输出格式Sparta|896
  50.     }
  51.     
  52.     function lookupAjax(){
  53.          var oSuggest = $("#username")[0].autocompleter;
  54.              oSuggest.findValue();
  55.            return false;
  56.            }
  57.     
  58.     </script>
  59. </head>
  60. <body>
  61.    
  62.     <p>
  63.         <label>
  64.             Month (local):</label>
  65.         <input type="text" id="month" />
  66.     </p>
  67.     <p>
  68.          </p>
  69.     <br />
  70.     <br />
  71.     <p>
  72.          </p>
  73.     <br />
  74.     <br />
  75.     <p>
  76.         <label>
  77.             E-Mail (local):</label>
  78.         <input type="text" id="suggest13" />
  79.     </p>
  80.     <p>
  81.          </p>
  82.     <p>
  83.         <label>
  84.            用户名测试:</label>
  85.         <input type="text" id="username" autocomplete="off" />
  86.     </p>
  87. </body>
  88. </html>
  89. ————————————————————————————————————————————————————
  90. using System;
  91. using System.Collections;
  92. using System.Configuration;
  93. using System.Data;
  94. using System.Linq;
  95. using System.Web;
  96. using System.Web.Security;
  97. using System.Web.UI;
  98. using System.Web.UI.HtmlControls;
  99. using System.Web.UI.WebControls;
  100. using System.Web.UI.WebControls.WebParts;
  101. using System.Xml.Linq;
  102. public partial class AutoTip : System.Web.UI.Page
  103. {
  104.     protected void Page_Load(object sender, EventArgs e)
  105.     {
  106.         if (!this.IsPostBack)
  107.         {
  108.             if (Request.QueryString["q"] != null && Request.QueryString["q"] != "")
  109.             {
  110.                 Response.Clear();
  111.                 Response.Charset = "utf-8";
  112.                 Response.Buffer = true;
  113.                 this.EnableViewState = false;
  114.                 Response.ContentEncoding = System.Text.Encoding.UTF8;
  115.                 Response.ContentType = "text/plain";
  116.                 Response.Write(GetLikeUserName(Request.QueryString["q"]));
  117.                 Response.Flush();
  118.                 Response.Close();
  119.                 Response.End();
  120.             }
  121.         }
  122.     }
  123.     private String GetLikeUserName(String namestr)
  124.     {
  125.         string strSql = "select name from autoTest where name " +
  126.                         "like '" + namestr + "%" + "' order by name";
  127.         DataTable dtQuestions = FillDataTable(strSql);
  128.         var sbstr = new System.Text.StringBuilder();
  129.         foreach (DataRow row in dtQuestions.Rows)
  130.         {
  131.             sbstr.Append(row["name"].ToString());
  132.             sbstr.Append("/n");
  133.         }
  134.         return sbstr.ToString();
  135.     }
  136.     public DataTable FillDataTable(string strQuery)
  137.     {
  138.         DataSet ds = myJQuery.DBUtility.DbHelperSQL.Query(strQuery);
  139.         return ds.Tables[0];
  140.     }
  141. }
  142. ——————————————————————————————————————————----
  143. AutoTip.aspx.cs部分
  144. using System;
  145. using System.Collections;
  146. using System.Configuration;
  147. using System.Data;
  148. using System.Linq;
  149. using System.Web;
  150. using System.Web.Security;
  151. using System.Web.UI;
  152. using System.Web.UI.HtmlControls;
  153. using System.Web.UI.WebControls;
  154. using System.Web.UI.WebControls.WebParts;
  155. using System.Xml.Linq;
  156. public partial class AutoTip : System.Web.UI.Page
  157. {
  158.     protected void Page_Load(object sender, EventArgs e)
  159.     {
  160.         if (!this.IsPostBack)
  161.         {
  162.             if (Request.QueryString["q"] != null && Request.QueryString["q"] != "")
  163.             {
  164.                 Response.Clear();
  165.                 Response.Charset = "utf-8";
  166.                 Response.Buffer = true;
  167.                 this.EnableViewState = false;
  168.                 Response.ContentEncoding = System.Text.Encoding.UTF8;
  169.                 Response.ContentType = "text/plain";
  170.                 Response.Write(GetLikeUserName(Request.QueryString["q"]));
  171.                 Response.Flush();
  172.                 Response.Close();
  173.                 Response.End();
  174.             }
  175.         }
  176.     }
  177.     private String GetLikeUserName(String namestr)
  178.     {
  179.         string strSql = "select name from autoTest where name " +
  180.                         "like '" + namestr + "%" + "' order by name";
  181.         DataTable dtQuestions = FillDataTable(strSql);
  182.         var sbstr = new System.Text.StringBuilder();
  183.         foreach (DataRow row in dtQuestions.Rows)
  184.         {
  185.             sbstr.Append(row["name"].ToString());
  186.             sbstr.Append("/n");
  187.         }
  188.         return sbstr.ToString();
  189.     }
  190.     public DataTable FillDataTable(string strQuery)
  191.     {
  192.         DataSet ds = myJQuery.DBUtility.DbHelperSQL.Query(strQuery);
  193.         return ds.Tables[0];
  194.     }
  195. }