Ajax: autocompleteextender is automatically Input

Source: Internet
Author: User
The autocompleteextender control can help you automatically enter the Textbox Control (search in the database ).
Attribute:
Targetcontrolid: Specifies " Automatic Input complete " The ID of the Textbox Control to be extended by the extender.
Servicepath: Path of the web service.
Servicemehod: Method Name of the Web service to be called. The method signature is as follows:

[System. Web. Services. webmethod]
[System. Web. Script. Service. scriptmethod]
Public   String [] Getcompetionlist ( String Prefixtext, Int Count ){......}


Front-endCode:

<% @ Page Language = " C # " Autoeventwireup = " True " Codefile = " Default. aspx. CS " Inherits = " _ Default "   %>

<% @ Register assembly = " Ajaxcontroltoolkit " Namespace = " Ajaxcontroltoolkit " Tagprefix = " PC3 "   %>

<! Doctype HTML public " -// W3C // dtd xhtml 1.1 // en "   " Http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd " >
< HTML xmlns = " Http://www.w3.org/1999/xhtml " >
< Head runat = " Server " >
< Title > Untitled page </ Title >
</ Head >
< Body >
< Form ID = " Form1 " Runat = " Server " >
< ASP: scriptmanager ID = " Scriptmanager1 " Runat = " Server "   />
< Div >
< ASP: textbox ID = " Textbox1 " Runat = " Server " > </ ASP: textbox >
< C0: autocompleteextender ID = " Autocompleteextender1 " Runat = " Server " Targetcontrolid = " Textbox1 "
Minimumprefixlength = " 1 " Servicemethod = " Getproductname " Servicepath = " WebService. asmx " >
</ C0: autocompleteextender >
</ Div >
</ Form >
</ Body >
</ Html >

WebService code:

Using System;
Using System. Web;
Using System. collections;
Using System. Web. Services;
Using System. Web. Services. Protocols;
Using System. Data;
Using System. Data. sqlclient;
Using System. Web. Script. Services; // KeyProgramSet Reference
Using System. Collections. Generic; // Key Assembly reference

///   <Summary>
/// Summary of WebService
///   </Summary>
[WebService (namespace =   " Http://tempuri.org/ " )]
[Webservicebinding (conformsto = Wsiprofiles. basicprofile1_1)]
[Scriptservice ()] // Be sure to add
Public   Class WebService: system. Web. Services. WebService {

Public WebService (){

// If you use the designed components, uncomment the following lines:
// Initializecomponent ();
}

[Webmethod]
[Scriptmethod]
Public   String [] Getproductname ( String Prefixtext, Int Count)
{
List < String > Suggestions = New List < String > (); // Declare a generic set
Sqlconnection con =   New Sqlconnection ( " Server =.; database = northwind; uid = sa; Pwd =; " );
Con. open ();
Sqlcommand com =   New Sqlcommand ( " Select distinct productname from products where productname like @ prefixname order by productname " , Con );
Com. Parameters. Add ( " @ Prefixname " , Sqldbtype. nvarchar). Value = Prefixtext +   " % " ;
Sqldatareader SDR = Com. executereader ();
While (SDR. Read ())
{
Suggestions. Add (SDR. getstring ( 0 ));
}
SDR. Close ();
Con. Close ();
Return Suggestions. toarray ();
}
}

This control is useful. When we use a search engine and enter a few characters, we can see relevant text prompts. It should be the application of Ajax technology. You can quickly select the content you want to input for textbox to reduce the input workload.

Attributes of the autocompleteextender control:
1. targetcontrolid: ID of the control that will be automatically input by the secondary node. The control can only be textbox;
2. servicepath: indicates the path of the web service that provides the service. If this parameter is not specified, servicemethod indicates the method name on the page;
3. servicemethod: indicates the method name for providing the service;
4. minimumprefixlength: specifies the minimum number of characters that the Textbox Control should have when the prompt service is provided. The default value is 3;
5. completionsetcount: Number of displayed items. The default value is 10;
6. enablecaching: whether to cache data on the client. The default value is true;
7. completioninterval: the time interval for reading data from the server. The default value is 1000. Unit: milliseconds.

All methods to provide services for the autocompleteextender control must meet the following three conditions:
A. The return type of the method must be string [];
B. The input parameter type of the method must be string or Int;
C. The two input parameter names must be prefixtext and count.

This article Reprinted from: http://daweijsp.javaeye.com/blog/398446,http://jialeifei.blog.163.com/blog/static/22190126200911810431891/

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.