AutoComplete of AjaxControlTookit Series

Source: Internet
Author: User

Here we need to implement the AutoComplete function of AjaxControlTookit. This function is used in common page searches and is very simple to use. However, if you want to expand it, you have to find another solution. Here we will only introduce how the AutoCompleteExtender control is implemented.

Let's take a look:

Page:

 <Form id = "form1" runat = "server">
<Asp: ScriptManager ID = "ScriptManager1" runat = "server">
</Asp: ScriptManager>
<Asp: UpdatePanel ID = "UpdatePanel1" runat = "server">
<ContentTemplate>
Author: <asp: TextBox ID = "TextBox1" runat = "server"> </asp: TextBox>
<Asp: Button ID = "Button1" runat = "server" Text = "query" OnClick = "button#click"/>
<Br/>
<AjaxToolkit: AutoCompleteExtender ID = "AutoCompleteExtender1" runat = "server" MinimumPrefixLength = "1"
TargetControlID = "TextBox1" ServiceMethod = "GetSearchWriter" ServicePath = "GetItems. asmx"
Completioninterval = "1000" enablecaching = "false">
</Ajaxtoolkit: autocompleteextender>
</Contenttemplate>
</ASP: updatepanel>
</Form>

The page setting is very simple, that is, drag a scriptmanager, then an updatepanel, and then drag an autocompleteextender to the contenttemplate. Here we mainly want to set the attributes in autocompleteextender, which is easy to understand, you will understand it at a Glance. I don't need to say more.

Next, create a web service, such as getitems. asmx. Its code is as follows:

 

 using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Web.Script.Services;
using System.Configuration;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class GetItems : System.Web.Services.WebService {
[WebMethod]
[ScriptMethod]
public string[] GetSearchWriter(string prefixText, int count)
{
try
{
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["Sql2005Express"].ConnectionString);
string sql = "SELECT DISTINCT TOP " + count + " newswriter FROM testNews WHERE newswriter like '%" + prefixText + "%' ";
SqlCommand cmd = new SqlCommand(sql, cn);
List<string> listnews = new List<string>();
cn.Open();

//newswriter
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
listnews.Add(dr[0].ToString());
}
dr.Close();
cn.Close();
return listnews.ToArray();
}
catch (Exception ex)
{
throw ex;
}

}


}

Note the following:

1. MARK [scriptservice] before the class

2. MARK [scriptmethod] before the method.

3. Do not change the parameter name, that is, string prefixtext, int count

You can achieve the above. If you have not tried it, please try it !~.

If you have better ideas or comments, you can leave a message.

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.