Ajax controltoolkit Learning Log-autocompleteextender (4)

Source: Internet
Author: User
The autocompleteextender control is used to populate the textbox with prompts. Like Google suggest. Here is a small example.

Example: Enter characters in the text box. Some optional lists are randomly displayed in the text box.

1) Create an ASP. NET Ajax-enabled web project. Name it autocompleteextender1.

2) Add the following in the source on the default. aspx page:Code:

1 < Div >
2 < C0: autocompleteextender ID = "Autocompleteextender1" Targetcontrolid = "Textbox1" Completionsetcount = "12"  
3 Servicepath = "Autocomplete. asmx" Servicemethod = "Getcompletionlist" Completioninterval = "1000" Enablecaching = "True" Minimumprefixlength = "2" Runat = "Server" >
4 </ C0: autocompleteextender >
5
6 </ Div >
7 < ASP: textbox ID = "Textbox1" Runat = "Server" > </ ASP: textbox >

3) Add a web service named autocomplete. asmx to the project and add the following code:

1 [System. Web. Script. Services. scriptservice ()]
2 Public   Class AutoComplete: system. Web. Services. WebService
3 {
4
5 [Webmethod]
6 Public   String [] Getcompletionlist ( String Prefixtext, Int Count)
7 {
8 If (Count =   0 )
9 {
10Count= 10;
11}
12
13 If (Prefixtext. Equals ( " Xyx " ))
14 {
15Return New String[0];
16}
17
18 List < String > Items = New List < String > (Count );
19 Random random1 = New Random ();
20 For ( Int I = 0 ; I < Count; I ++ )
21 {
22 Char C1 = ( Char ) Random1.next ( 65 , 97 );
23 Char C2 = ( Char ) Random1.next ( 97 , 122 );
24 Char C3 = ( Char ) Random1.next ( 97 , 122 );
25 Items. Add (prefixtext + C1 + C2 + C3 );
26 }
27 Return Items. toarray ();
28 }
29 }

4) Save and press Ctrl + F5 to preview in the browser. The effect is as follows:

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.