ASP.NET輸入文字框自動提示功能_實用技巧

來源:互聯網
上載者:User

在ASP.NET Web開發中會經常用到自動提示功能,比如百度搜尋。我們只要輸入相應的關鍵字,就可以自動得到相似搜尋索引鍵的提示,方便我們快速的輸入關鍵字進行查詢。

那麼在ASP.NET中,如果我們需要做個類似的效果,該如何做到呢?
很簡單,我們只要藉助於一個JQuery強大的外掛程式JQuery AutoComplete來完成這個效果就可以了。這個外掛程式的官方地址為:JQuery AutoComplete,裡面也有範例程式碼。
下面我們將以一個社會安全號碼自動查詢為例,來看看JQuery AutoComplete的強大和簡潔。
首先我們要準備好外掛程式,可以在官方下面下載下來。
一、aspx頁面
在head部分,匯入相應js和css。

<script src="../js/jquery-1.4.2.js" type="text/javascript"></script>  <link href="../js/jquery.autocomplete.css" rel="stylesheet" type="text/css" />  <script src="../js/jquery.autocomplete.js" type="text/javascript"></script> 

注意jquery-1.4.2.js一定要在最上面,因為autocomplete外掛程式基於核心jquery.js。至於jquery的版本,讀者可以自行下載最新版。
然後繼續寫上核心js部分。

<script type="text/javascript">   $(function(){     $("#<%=txtSfzh.ClientID %>").autocomplete("../services/SearchSyryInfoService.ashx",{       width: 500,       max: 20,       delay: 5,       cacheLength: 1,       formatItem: function(data, i, max) {         return data.toString();       },       formatResult: function(data) {         return data.toString().split(",")[1];       }     }).result(function(event, data, formatted) {       var array = data.toString().split(",");       $("#<%=txtXm.ClientID %>").val(array[0]);//姓名       $("#<%=txtSfzh.ClientID %>").val(array[1]);//社會安全號碼       $("#<%=txtJtzz.ClientID %>").val(array[2]);//家庭住址       $("#<%=txtLxdh.ClientID %>").val(array[3]);//聯絡電話     });   }); </script> 

在body的頁面部分準備一個頁面:

<table cellpadding="0" cellspacing="0" border="1" width="100%">       <tr>         <td>           <label>             社會安全號碼</label>         </td>         <td>           <asp:TextBox runat="server" ID="txtSfzh" />         </td>         <td>           <label>             姓名</label>         </td>         <td>           <asp:TextBox runat="server" ID="txtXm" />         </td>       </tr>       <tr>         <td>           <label>             家庭地址</label>         </td>         <td>           <asp:TextBox runat="server" ID="txtJtzz" />         </td>         <td>           <label>             聯絡電話</label>         </td>         <td>           <asp:TextBox runat="server" ID="txtLxdh" />         </td>       </tr>       <tr align="center">         <td colspan="4">           <asp:Button ID="btnSearch" runat="server" Text="查詢" Width="80px" OnClick="btnSearch_Click" />            <asp:Button ID="btnReset" runat="server" Text="重設" Width="80px" OnClick="btnReset_Click" />         </td>       </tr>     </table> 

二、ashx後台

public void ProcessRequest(HttpContext context)   {     context.Response.ContentType = "text/plain";      if (context.Request.QueryString["q"] != null)     {       string key = context.Request.QueryString["q"];       if (key.Trim().Length >= 8)//大於等於8位,才去查資料庫。這是為了緩解資料庫查詢的壓力,只當輸入了8位以上身份證以後才進行資料庫檢索。       {         string keyValues = GetKeyValues(key);         context.Response.Write(keyValues);       }     }   }    public bool IsReusable   {     get     {       return false;     }   }    public static string GetKeyValues(string key)   {     BLL bll = new BLL();     DataTable dt = bll.GetPersons(key).Tables[0];//通過關鍵字k(k是前台頁面輸入的社會安全號碼碼)到後台去查詢人員資訊並返回一個結果集     StringBuilder sb = new StringBuilder();     foreach (DataRow dr in dt.Rows)     {       sb.Append(dr["result"].ToString() + "\n");     }     return sb.ToString().Trim();   } 

如上代碼即可實現輸入社會安全號碼時自動檢索資料庫並給出相關資訊,當選擇某條資料的時候,自動給文字框賦值,減少了人工的輸入。

以上就是本文的全部內容,希望對大家的學習有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.