Ajax framework autocompleteextender for Automatic completion
If I need a WebService, I am too lazy to rename it. It is called WebService. asmx. Why do I need WebService? In fact, I don't know. I only know that AutoCompleteExtender requires three key attributes:
ServicePath = "WebService. asmx"
ServiceMethod = "GetWordList"
TargetControlID = "txtText"
If you know these three attributes, you may know why you want to use WebService. ServicePath is the path of WebService and the method name in ServiceMethod: WebService, targetControlID is the control to be automatically completed (it is a bit unclear, but you can understand what it means );
The Code is as follows:
View Code using System;
Using System. Web. Services;
Using System. Data;
Using System. Data. SqlClient;
Using CommonUtility;
Namespace GridView warehouse receiving ticket management
{
/// <Summary>
/// Summary of WebService
/// </Summary>
[WebService (Namespace = "http://tempuri.org/")]
[WebServiceBinding (ConformsTo = WsiProfiles. BasicProfile1_1)]
[System. ComponentModel. ToolboxItem (false)]
// To allow ASP. net ajax to call this Web service from a script, cancel the comments to the downstream.
[System. Web. Script. Services. ScriptService]
Public class WebService: System. Web. Services. WebService
{
[WebMethod]
Public String [] GetWordList (string prefixText, int count)
{
String SQL = string. Format ("select top {0} * from InBill where saleName like '%" + @ prefixText + "%'", @ count );
SqlParameter [] paras = new SqlParameter []
{
New SqlParameter ("@ prefixText", prefixText ),
New SqlParameter ("@ count", count)
};
DataTable table = SQLHelper. GetDateSet (SQL, CommandType. Text, paras );
String [] arr = new string [table. Rows. Count];
If (table! = Null)
{
For (int I = 0; I <table. Rows. Count; I ++)
{
Arr [I] = table. Rows [I] ["saleName"]. ToString ();
}
}
Return arr;
}
}
}
SQL statement: In the fame method, Count is used at this time. When completeextender adds CompletionSetCount = "5", it is useful. What does it mean? The number of data entries displayed when the task is automatically completed. If this parameter is not set, the default value is 10. That is to say, 10 data entries are displayed in the drop-down list. If this parameter is set, here we can pass Count;
String SQL = string. Format ("select top {0} * from InBill where saleName like '%" + @ prefixText + "%'", @ count );
String [] arr = new string [table. rows. count]; // defines an array of the string type, so that its length is equal to the number of rows in the table we found; then we need to traverse the table and fill the data in each row into the array;
Arr [I] = table. rows [I] ["saleName"]. toString (); saleName is the field name in the database tutorial. Which field is bound to you here, and the value of which field will be displayed when Automatic completion;
Code snippet of the Aspx page:
<Asp Tutorial: ScriptManager ID = "ScriptManager1" runat = "server"/>
<Asp: AutoCompleteExtender ID = "AutoCompleteExtender1" runat = "server" MinimumPrefixLength = "1"
CompletionInterval = "500" EnableCaching = "false" ServiceMethod = "GetWordList" ServicePath = "WebService. asmx"
TargetControlID = "txtText" CompletionSetCount = "5"/>
<Asp: TextBox ID = "txtText" runat = "server"> </asp: TextBox>
First, a ScriptManager is required. The following describes the meaning of each attribute in AutoCompleteExtender;
MinimumPrefixLength: it is automatically completed when the minimum number of characters is entered;
CompletionInterval: The Automatic completion interval;
EnableCaching: Indicates whether to enable caching;
ServiceMethod: method name in WebService;
ServicePath: WebService path;
TargetControlID: The bound control;
CompletionSetCount: displays the number of automatically completed rows;
A detailed instance
Function:
The TextBox Control can be used for automatic input, for example, when searching in google.
Attribute:
TargetControlID: Specifies the Control ID that will be automatically entered by the secondary node. The control can only be TextBox;
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;
ServiceMethod: Specifies the method name for providing the service;
MinimumPrefixLength: specifies the minimum number of characters that the TextBox Control should have when the prompt service is provided. The default value is 3;
CompletionSetCount: Number of displayed items. The default value is 10;
EnableCaching: whether to cache data on the client. The default value is true;
CompletionInterval: the time interval for reading data from the server. The default value is 1000. Unit: milliseconds.
Code example:
ASPX page code:
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> AutoComplete server control </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Asp: ScriptManager runat = "server" ID = "ScriptManager1"/>
<PC3: AutoCompleteExtender
ID = "AutoCompleteExtender1"
Runat = "server"
ServicePath = "AutoComplete. asmx"
TargetControlID = "TextBox1"
ServiceMethod = "GetWordList"
MinimumPrefixLength = "1"
EnableCaching = "true"
CompletionSetCount = "12"
CompletionInterval = "1000">
</PC3: AutoCompleteExtender>
<Asp: TextBox ID = "TextBox1" runat = "server"> </asp: TextBox>
</Form>
</Body>
</Html>
Compile corresponding webservices
Public class AutoComplete: System. Web. Services. WebService {
Public AutoComplete (){
// Uncomment the following line if using designed components
// InitializeComponent ();
}
Private static string [] autoCompleteWordList = null;
[WebMethod]
Public String [] GetWordList (string prefixText, int count)
{
If (autoCompleteWordList = null)
{
String [] temp = File. ReadAllLines (Server. MapPath ("~ /App_Data/words.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;
}
}
Pay attention to the following points:
1. Because the WEB Service provides services for the Ajax framework, you must add the attribute declaration before the class declaration:
[System. Web. Script. Services. ScriptService]
2. Pay special attention to the GetTextString method. 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.
Enter words.txt under app_data.
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