1、引用组件AjaxControlToolkit.dll
2、Web.config中添加对该控件的引用
<pages>
<controls>
<add namespace="AjaxControlToolkit" assembly="AjaxControlToolkit" tagPrefix="ajaxToolkit"/>
controls>
pages>
注意:如果不在这里设置,则要在aspx页面设置下面写法:
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
3、添加服务设置文件AutoComplete.asmx
里面不需要编辑什么内容,保留自动生成的代码即可。
其代码文件AutoComplete.cs会放到App_Code文件夹下
内部代码如下:
using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Data.OleDb;
using System.Data;
///
///AutoComplete 的摘要说明
///
[WebService(Namespace = "")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释(默认是不取消的)。
[System.Web.Script.Services.ScriptService]
public class AutoComplete : System.Web.Services.WebService
{
private string[] autoCompleteWordList;
//下面颜色字体,除了方法名字可以改变外,其他不能变,固定写法返回字符串数组。
[WebMethod]
public String[] GetCompleteList(string prefixText, int count)
{
string[] autoCompleteWordList = null;
if (string.IsNullOrEmpty(prefixText) || count <= 0)
{
return null;
}
if (autoCompleteWordList == null)
{
OleDbConnection connection =
new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
Server.MapPath("AutoComplete.mdb"));
connection.Open();
OleDbDataAdapter adapter =
new OleDbDataAdapter(
"SELECT keyName FROM keyInfo WHERE keyName LIKE'" + prefixText + "%' ORDER BY keyName", connection);
DataSet dataSet = new DataSet();
adapter.Fill(dataSet);
autoCompleteWordList = new string[dataSet.Tables[0].Rows.Count];
int i = 0;
foreach (DataRow dr in dataSet.Tables[0].Rows)
{
autoCompleteWordList[i] = dr["keyName"].ToString();
i++;
}
if (connection.State == ConnectionState.Open)
connection.Close();
}
return autoCompleteWordList;
}
}
5、页面代码中调用代码
http://tzluxiantao.blog.163.com/blog/static/859928201171910421872/
阅读(255) | 评论(0) | 转发(0) |