Home >Web Front-end >JS Tutorial >Usage of ajaxControlToolkit AutoCompleteExtender_javascript skills

Usage of ajaxControlToolkit AutoCompleteExtender_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:59:181092browse

AutoCompleteExtender automatically completes the extension. When used with TextBox, the function is similar to entering search words in Google, and a drop-down box will appear under TextBox to display the items in the search target
This extension control needs to be used in conjunction with Web Service, so it involves the use of web Service. (I will only talk about it briefly here, and then I will talk about the content of web service in detail after you are familiar with it)
First introduce several key attributes of AutoCompleteExtender:
a, TargetControlID This attribute is a common attribute of all AjaxControlToolkit, which is an extension. Target control ID (this is what the official said)
b.CompletionSetCount This attribute is to set the number of drop-down results to be displayed. The default is 10
c.MinimumPrefixTextLength This attribute is to set the length of a few characters and call the webService. The method displays a drop-down list
d.ServicePath This attribute sets the path of the web service that needs to be called
e.ServiceMethod This attribute sets the method (function) in the web service that needs to be called
f.EnableCaching: whether it is in the client Cache data on the end, the default is true
g.CompletionInterval: the time interval for reading data from the server, the default is 1000, unit: milliseconds
Note: If you are used to setting properties with visual controls, the a property is in AutoCompleteExtender After setting the TargetControlId, other properties will be set in the Extenders property in the corresponding TargetControl. If you are used to handwriting code, set it in the AutoCompleteExtender code property.
Example: 1. Create a new page, add a ScriptManager control, a TextBox control and an AutoCompleteExtender control
2. Create a new webService and add a [WebMethod] method
[WebMethod]

Copy code The code is as follows:

public string[] GetString(string prefixText, int count){
System.Collections. Generic.List list = new System.Collections.Generic.List(count);
System.Data.DataSet ds = new System.Data.DataSet();
//This is me Code for fetching data from the database. The SqlHelper class is the base class for fetching data in the project
//string strSql = string.Format("SELECT TOP {0} NAME FROM CengWei WHERE NAME LIKE '{1}%' ORDER BY NAME",count,prefixText);
//ds = SqlHelper.Query(strSql);
//for (int i = 0; i < ds.Tables[0].Rows.Count; i )
//{
// list.Add(ds.Tables[0].Rows[i][0].ToString());
//}
for (int i = 0 ; i < count; i )
{
list.Add(prefixText i.ToString());
}
return list.ToArray();
}

Among them:
[System.Web.Script.Services.ScriptService] must be added to the webService class
Sample code: webService fetches data from a field in the database
Page code:
Copy code The code is as follows:

<%@ Page Language="C#" AutoEventWireup= "true" CodeFile="test2.aspx.cs" Inherits="test2" %>
<%@ Register Assembly="CrystalDecisions.Web, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"
Namespace="CrystalDecisions.Web" TagPrefix="CR" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>



DropDownExtender simple exercise
< link href="/aspnet_client/System_Web/2_0_50727/CrystalReportWebFormViewer3/css/default.css"
rel="stylesheet" type="text/css" />

< body>






ServiceMethod=" GetString" ServicePath="AutoComplete.asmx" TargetControlID="TextBox2">





webService code:
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
///
/// Summary description of AutoComplete
///

[WebService(Namespace = "http://tempuri.org/") ]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//The following is required, otherwise the function cannot be implemented
[System.Web.Script.Services.ScriptService]
public class AutoComplete : System .Web.Services.WebService {
public AutoComplete () {
//Uncomment the following lines if using designed components
//InitializeComponent();
}
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
[WebMethod]
public string[] GetString(string prefixText, int count){
System.Collections .Generic.List list = new System.Collections.Generic.List(count);
System.Data.DataSet ds = new System.Data.DataSet();
//Here is My code for fetching data from the database. The SqlHelper class is the base class for fetching data in the project
//string strSql = string.Format("SELECT TOP {0} NAME FROM CengWei WHERE NAME LIKE '{1}%' ORDER BY NAME",count,prefixText);
//ds = SqlHelper.Query(strSql);
//for (int i = 0; i < ds.Tables[0].Rows.Count; i )
//{
// list.Add(ds.Tables[0].Rows[i][0].ToString());
//}
for (int i = 0; i < count; i )
{
list.Add(prefixText i.ToString());
}
return list.ToArray();
}
}
Please tell me if there is anything wrong
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn