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.