The use of AjaxControlToolkit to achieve Baidu search when the Drop-down list prompts detailed steps _ajax related

Source: Internet
Author: User
Tags time interval
AjaxControlToolkit is a set of controls, you can automatically add text boxes, click the text box pop-up calendar, add watermark and other Ajax effects, including more than 40 controls, the implementation of the effect such as: http://www.asp.net/ajaxLibrary/ Ajaxcontroltoolkitsamplesite/default.aspx
Like Baidu search, according to user Input Automatic association vocabulary, with the help of AjaxControlToolkit Autocompleteextender control very simple implementation, the results are as follows:
Detailed steps:
One: vs. install AjaxControlToolkit
AjaxControlToolkit installs to VS (requires attention to version issues):
Installation method: Http://www.asp.net/ajaxlibrary/act.ashx
Appropriate version hint: http://ajaxcontroltoolkit.codeplex.com/
second: Calling Autocompleteextender in a Web page(Register ahead of the page, second line of code)
Copy Code code as follows:

<%@ Page language= "C #" autoeventwireup= "true" codebehind= WebForm1.aspx.cs "inherits=" Html_editor. WebForm1 "%>
<%@ Register tagprefix= "asp" namespace= "AjaxControlToolkit" assembly= "AjaxControlToolkit"%>
<! DOCTYPE html>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title></title>
<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:toolkitscriptmanager id= "ToolkitScriptManager1" runat= "Server"/>
<asp:textbox id= "TextBox1" runat= "Server" ></asp:TextBox>
<asp:autocompleteextender id= "AutoCompleteExtender1" runat= "Server"
Targetcontrolid= "TextBox1"
Completionsetcount= "10"
Enablecaching= "true"
Minimumprefixlength= "1"
completioninterval= "100"
Servicepath= "Webservice.asmx"
servicemethod= "Getenglishname" >
</asp:AutoCompleteExtender>
</div>
</form>
</body>

Three: Add Web Services Webservice.asmx
Copy Code code as follows:

Using System;
Using System.Collections.Generic;
Using System.Data.SqlClient;
Using System.Linq;
Using System.Web;
Using System.Web.Script.Services;
Using System.Web.Services;
Namespace Html_editor
{
<summary>
Summary description of WebService
</summary>
[WebService (Namespace = "http://tempuri.org/")]
[WebServiceBinding (ConformsTo = wsiprofiles.basicprofile1_1)]
[System.ComponentModel.ToolboxItem (False)]
To allow the use of ASP.net AJAX to invoke this Web service from a script, uncomment the line.
[System.Web.Script.Services.ScriptService]
public class WebService:System.Web.Services.WebService
{
Reading matching information from the database
[WebMethod]
[Scriptmethod]
Public string[] Getenglishname (string prefixtext, int count)
{
list<string> suggestions = new list<string> ()//declaration of a generic collection
SqlConnection con = new SqlConnection ("server=.; database=attendance;uid=sa;pwd=; ");
Con. Open ();
SqlCommand com = new SqlCommand ("SELECT [Englishname] from [Employee] where [englishname] like '%t% ' ORDER by [Englishnam E] ", con);
SqlDataReader SDR = com. ExecuteReader ();
while (SDR. Read ())
{
Suggestions. ADD (SDR. GetString (0));
}
Sdr. Close ();
Con. Close ();
return suggestions. ToArray ();
}
Direct use of methods to generate matching information
[WebMethod]
Public string[] Getcompletelist (string prefixtext, int count)
//{
Char C1, C2, C3;
if (count = = 0)
Count = 10;
list<string> list = new list<string> (count);
Random rnd = new Random ();
for (int i = 1; I <= count; i++)
// {
C1 = (char) rnd. Next (65, 90);
C2 = (char) rnd. Next (97, 122);
C3 = (char) rnd. Next (97, 122);
List. ADD (Prefixtext + c1 + C2 + C3);
// }
Return list. ToArray ();
//}
}
}

Four: Complete, run the Web page can see the text box automatically add effect, need to pay attention to the following places
Autocompleteextender Control Parameter Description:
1.TargetControlID: Specifies the control to implement the hint function;
2.servicepath:webservice path, the method of extracting data is written in a webservice;
3.ServeiceMethod: The name of the method used to extract the data written in the WebService;
4.MinimumPrefixLength: To set the user input how many letters to appear prompt effect;
5.CompletionSetCount: Set the number of rows to prompt the data;
6.CompletionInterval: The time interval, in milliseconds, to get the book from the server.
Webservice.asmx need to pay attention to the place:
1. Because the Web service is serviced for the AJAX framework, you must precede the class declaration with a property declaration:
[System.Web.Script.Services.ScriptService]
2. Special attention is paid to the gettextstring approach. Any method that provides services to a Autocompleteextender control must fully meet the following three conditions:
A. The return type of the method must be: string [];
B. The incoming parameter type of the method must be: string, int;
C. Two incoming parameter names must be: Prefixtext, count.
The value entered in the text box is passed to the WebService:
aspx
Copy Code code as follows:

<%@ Page language= "C #" autoeventwireup= "true" codefile= "TestSearch.aspx.cs" inherits= "Testsearch"%>
<%@ Register tagprefix= "asp" namespace= "AjaxControlToolkit" assembly= "AjaxControlToolkit"%>
<! DOCTYPE html>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title></title>
<script type= "Text/javascript" >
function Ontxtpersoninfokeydown () {
var val = document.getElementById ("<%=textbox1.clientid%>"). Value;
var Nameclientid = "<%=autocompleteextender1.clientid%>";
var acname = $find (Nameclientid);
if (acname!= null) {
Acname.set_contextkey (Val);
}
}
function OnTxtPersonInfoKeyDown2 () {
var val = document.getElementById ("<%=textbox2.clientid%>"). Value;
var Nameclientid = "<%=autocompleteextender2.clientid%>";
var acname = $find (Nameclientid);
if (acname!= null) {
Acname.set_contextkey (Val);
}
}
</script>
<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:toolkitscriptmanager id= "ToolkitScriptManager1" runat= "Server"/>
<asp:textbox id= "TextBox1" runat= "Server" ></asp:TextBox>
<asp:autocompleteextender id= "AutoCompleteExtender1" runat= "Server"
Targetcontrolid= "TextBox1"
Completionsetcount= "10"
Enablecaching= "false"
Firstrowselected= "true"
usecontextkey= "True"
Minimumprefixlength= "0"
completioninterval= "100"
Servicepath= "Webservice.asmx"
servicemethod= "Getenglishname" >
</asp:AutoCompleteExtender>
<br/>
<asp:textbox id= "TextBox2" runat= "Server" ></asp:TextBox>
<asp:autocompleteextender id= "AutoCompleteExtender2" runat= "Server"
Targetcontrolid= "TextBox2"
Completionsetcount= "10"
Enablecaching= "false"
Firstrowselected= "true"
usecontextkey= "True"
Minimumprefixlength= "0"
completioninterval= "100"
Servicepath= "Webservice.asmx"
servicemethod= "Getenglishname" >
</asp:AutoCompleteExtender>
</div>
</form>
</body>

Aspx.cs
Copy Code code as follows:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
public partial class TestSearch:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
TEXTBOX1.ATTRIBUTES.ADD ("onkeydown", "return Ontxtpersoninfokeydown ();");
TEXTBOX2.ATTRIBUTES.ADD ("onkeydown", "return OnTxtPersonInfoKeyDown2 ();");
}
}

Webservice.asmx.cs
Copy Code code as follows:

<%@ WebService language= "C #" class= "WebService"%>
Using System;
Using System.Web;
Using System.Web.Services;
Using System.Web.Services.Protocols;
Using System.Web.Script.Services;
Using System.Data;
Using System.Data.SqlClient;
Using System.Collections.Generic;
[WebService (Namespace = "http://tempuri.org/")]
[WebServiceBinding (ConformsTo = wsiprofiles.basicprofile1_1)]
To allow the use of ASP.net AJAX to invoke this Web service from a script, uncomment the line.
[System.Web.Script.Services.ScriptService]
public class WebService:System.Web.Services.WebService
{
Reading matching information from the database
[WebMethod]
[Scriptmethod]
Public string[] Getenglishname (string prefixtext, int count, string contextkey)
{
SQLHelper SQLH = new SQLHelper ();
Contextkey = "T";
String strSQL = "SELECT [Englishname] from [Employee] where [leftdate] are null and [englishname] like '" + Contextkey + " % ' ORDER BY [Englishname] ';
DataTable dt = Sqlh.executequery (strSQL, CommandType.Text);
list<string> suggestions = new list<string> ()//declaration of a generic collection
Suggestions. Clear ();
if (dt. Rows.Count > 0)
{
for (int i = 0; i < dt. Rows.Count; i++)
{
Suggestions. ADD (dt. Rows[i][0]. ToString ());
}
}
return suggestions. ToArray ();
}
}

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.