Use the ASP. NET Ajax-filteredtextbox Control

Source: Internet
Author: User
Document directory
  • Introduction
  • Important attributes
  • Example

I used to see Google, thunder, and other websites entering text in the search text box can automatically prompt, I feel this function is very dazzling and practical. now I have learned Asp.net Ajax and found that the autocomplete control in the ajaxcontroltoolkit can implement this function, which is very simple.

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

Targetcontrolid: Specifies the control to implement the prompt function.

Servicepath: the path of the WebService. The data extraction method is written in a WebService.

Serveicemethod: name of the method used to extract data in WebService.

Minimumprefixlength: used to set the number of letters entered by the user to display the prompt effect.

Completionsetcount: set the number of rows of the prompt data.

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 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 abstract description /// </Summary> [WebService (namespace = "http://tempuri.org/")] [webservicebinding (conformsto = wsiprofiles. basicprofile1_1)] [system. web. script. services. scriptservice] public class AutoComplete: system. web. services. webService {pub LIC AutoComplete () {// if you use the designed component, uncomment the following line // initializecomponent ();} [webmethod] Public String helloworld () {return "Hello World ";} /// <summary> /// getcompletelist // </Summary> // defines 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. readal Llines (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 on 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.

Code download

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.