使用ASP.NET Atlas AutoComplete Behavior或AutoComplete Extender實現自動完成功能(下)

來源:互聯網
上載者:User
作者:Dflying Chen (http://dflying.cnblogs.com/)
請同時參考:使用ASP.NET Atlas AutoComplete Behavior或AutoComplete Extender實現自動完成功能(上)

讓我們通過一個執行個體來測試上述兩種方法:

首先,讓我們建立一個詞庫,提供自動完成的列表。這個詞庫Copy自Atlas的官方文檔樣本,是一些.NET的常見術語。將其存為WordData.txt共置於App_Data目錄下。

Word List
access control list (ACL)
ADO.NET
aggregate event
alpha channel
anchoring
antialiasing
application base
application domain (AppDomain)
application manifest
application state
ASP.NET
ASP.NET application services database
ASP.NET mobile controls
ASP.NET mobile Web Forms
ASP.NET page
ASP.NET server control
ASP.NET Web application
assembly
assembly cache
assembly manifest
assembly metadata
assertion (Assert)
association class
ASSOCIATORS OF
asynchronous method
attribute
authentication
authorization
autopostback
bounds
boxing
C#
card
catalog
CCW
chevron
chrome
cHTML
CIM
CIM Object Manager
CIM schema
class
client area
client coordinates
clip
closed generic type
CLR
CLS
CLS-compliant
code access security
code-behind class
code-behind file
code-behind page
COM callable wrapper (CCW)
COM interop
Common Information Model (CIM)
common language runtime
common language runtime host
Common Language Specification (CLS)
common object file format (COFF)
common type system (CTS)
comparison evaluator
composite control
configuration file
connection
connection point
constraint
constructed generic type
constructed type
consumer
container
container control
content page
context
context property
contract
control state
cross-page posting
CTS
custom attribute (Attribute)
custom control

然後建立一個Web Service用來提供建議列表,其中邏輯不多講了,大概是讀入上面的詞庫並根據輸入找出相關的詞彙,注意一下GetWordList方法的簽名。

Suggestion Web Service Code
using System;
using System.IO;
using System.Web;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Serialization;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class AutoCompleteService : System.Web.Services.WebService
{
    private static string[] autoCompleteWordList = null;

    [WebMethod]
    public String[] GetWordList(string prefixText, int count)
    {
        // init the suggest list
        if (autoCompleteWordList == null)
        {
            string[] temp = File.ReadAllLines(Server.MapPath("~/App_Data/WordData.txt"));
            Array.Sort(temp, new CaseInsensitiveComparer()); // sort for binary search
            autoCompleteWordList = temp;
        }

        int index = Array.BinarySearch(autoCompleteWordList, prefixText, new CaseInsensitiveComparer());
        if (index < 0)
        {
            index = ~index;
        }

        int matchingCount;
        for (matchingCount = 0; matchingCount < count && index + matchingCount < autoCompleteWordList.Length; matchingCount++)
        {
            if (!autoCompleteWordList[index + matchingCount].StartsWith(prefixText, StringComparison.CurrentCultureIgnoreCase))
                break;
        }

        String[] returnValue = new string[matchingCount];
        if (matchingCount > 0)
        {
            Array.Copy(autoCompleteWordList, index, returnValue, 0, matchingCount);
        }
        return returnValue;
    }
}

Web Service建立好之後您可以直接測試一下,如果一切正確,我們就繼續編寫Atlas頁面。

首先,無論使用用戶端AutoComplete Behavior還是伺服器端AutoComplete Extender,一個ScriptManager都是必不可少的:

<atlas:ScriptManager runat="server" ID="scriptManager" />

如果使用用戶端AutoComplete Behavior,首先需要書寫一個HTML input:

<input id="clientTextBox" type="text" style="width: 400px;" />

然後,相應的書寫Atlas Script。請小心書寫畢竟這裡基本沒有強大的IDE的支援。

<page xmlns:script="http://schemas.microsoft.com/xml-script/2005">
   <components>
       <textBox id="clientTextBox">
           <behaviors>
               <autoComplete 
                  serviceURL="AutoCompleteService.asmx"
                  serviceMethod="GetWordList"
                  minimumPrefixLength="2"
                  completionSetCount="10"
                  completionInterval="500" />
           </behaviors>
       </textBox>
   </components>
</page>

在使用伺服器端AutoComplete Extender時,一切都非常簡單,我們只需要添加一個伺服器端TextBox和一個AutoComplete Extender即可:

<asp:TextBox ID="serverTextbox" runat="server" Width="400px" />
<atlas:AutoCompleteExtender ID="serverCompleteExtender" runat="server">
    <atlas:AutoCompleteProperties Enabled="true" MinimumPrefixLength="2" TargetControlID="serverTextbox"
        ServiceMethod="GetWordList" ServicePath="AutoCompleteService.asmx" />
</atlas:AutoCompleteExtender>

至此為止,大功告成,讓我們在瀏覽器中測試一下:

用戶端AutoComplete Behavior:

伺服器端AutoComplete Extender:

本執行個體程式的原始碼可以在此下載:http://files.cnblogs.com/dflying/AutoCompleteDemo.zip

相關文章

聯繫我們

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