Use of the AutoComplete control in ASP. NET Ajax

Source: Internet
Author: User

Introduction

The AutoComplete control is used when you enter the first few letters or Chinese characters in the text box. This control can prompt you all the data starting with these letters from the text or database that stores the data, for your convenience.

Important attributes

1. TargetControlID: Specifies the control to implement the prompt function;

2. ServicePath: the path of the WebService. The data extraction method is written in a WebService;

3. ServeiceMethod: name of the method used to extract data in WebService;

4. MinimumPrefixLength: used to set the number of letters entered by the user to display the prompt effect;

5. CompletionSetCount: set the number of data rows prompted;

6. CompletionInterval: the time interval for obtaining books from the server, in milliseconds.

Example

Open vs2005 to create an AjaxControlToolKit website.

Upload the text file textfile.txt under the app_datafolder on the website, and add the data in it, as shown below:

Add a Web Service under the root directory of the website named oec2003_AutoComplete. The system automatically designs two parts: oec2003_AutoComplete.asmx and oec2003_AutoComplete.cs, the oec2003_AutoComplete.cs file is automatically placed in the App_Code directory. Open the oec2003_AutoComplete.cs file and add the GetCompleteList method for getting data. The Code is as follows:

Using System; using System. Web; using System. Collections; using System. Web. Services; using System. Web. Services. Protocols; using System. IO; // <summary>/// AutoComplete summary /// <summary>[WebService (Namespace = "http://tempuri.org/")] [WebServiceBinding (ConformsTo = WsiProfiles. basicProfile1_1)] [System. web. script. services. scriptService] public class AutoComplete: System. web. services. webService {public AutoComplete () {// If the designed component is used, uncomment the following line // InitializeComponent ();} [WebMethod] public string HelloWorld () {return "Hello World" ;}/// <summary>/// Method for obtaining data GetCompleteList /// <summary>// Define a static array to save the obtained data private static string [] autoCompleteWordList = null; [WebMethod] public String [] GetCompleteList (string prefixText, int count) {if (autoCompleteWordList = null) {string [] temp = File. readAllLines (Server. mapPath ("~ /App_Data/TextFile.txt "); Array. sort (temp, new CaseInsensitiveComparer (); 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 ;}}

Because the File class is used in the above Code, you should add the following code:

using System.IO;

Because you need to call the Web service on the client, you also need to add the following code:

[System.Web.Script.Services.ScriptService]

Save the Web Service Code

Open the Default. aspx generated by Default in the root directory

Drag a TextBox Control and an AutoCompleteExtender control to the page.

Set the attributes of the AutoCompleteExtender control in the Properties window, as shown below:

<ajaxToolkit:AutoCompleteExtender             ID="AutoCompleteExtender1"             runat="server"             ServiceMethod="GetCompleteList"             ServicePath="oec2003_AutoComplete.asmx"             Enabled="true"             MinimumPrefixLength="2"                CompletionSetCount="10"            TargetControlID="TextBox1"></ajaxToolkit:AutoCompleteExtender>

The value of the count parameter in the Web Service is the value of the CompletionSetCount attribute.

Save the design page and set the default page as the start page. Press F5 and enter oe in the text box to view the desired result.

  1. ASP. net mvc Case Study
  2. ASP. net mvc Tutorial: Create a TaskList Application
  3. Implementation of URL Rewrite in ASP. NET

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.