简介
AutoComplete控件就是在用户在文本框输入前几个字母或是汉字的时候,该控件就能从存放数据的文或是数据库里将所有以这些字母开头的数据提示给用户,供用户选择,提供方便。
重要属性
1.TargetControlID:指定要实现提示功能的控件;
2.ServicePath:WebService的路径,提取数据的方法是写在一个WebService中的;
3.ServeiceMethod:写在WebService中的用于提取数据的方法的名字;
4.MinimumPrefixLength:用来设置用户输入多少字母才出现提示效果;
5.CompletionSetCount:设置提示数据的行数;
6.CompletionInterval:从服务器获取书的时间间隔,单位是毫秒。
新建一个webservices文件,输入代码:
using System;
using System.Web;
using System.Collections;
using System.Collections.Generic;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.IO;
/// <summary>
/// AutoComplete 的摘要说明
/// <summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class AutoComplete : System.Web.Services.WebService
{
/// <summary>
/// 获取数据的方法GetSuggestions
/// <summary>
//定义静态数组用于保存获取的数据
private static string[] autoCompleteWordList = null;
[WebMethod]
public string[] GetSuggestions(string prefixText, int count)
{
List<string> responses = new List<string>();
for (int i = 0; i < count; i++)
responses.Add(prefixText + (char)(i + 65));
return responses.ToArray();
}
}
using System.Web;
using System.Collections;
using System.Collections.Generic;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.IO;
/// <summary>
/// AutoComplete 的摘要说明
/// <summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class AutoComplete : System.Web.Services.WebService
{
/// <summary>
/// 获取数据的方法GetSuggestions
/// <summary>
//定义静态数组用于保存获取的数据
private static string[] autoCompleteWordList = null;
[WebMethod]
public string[] GetSuggestions(string prefixText, int count)
{
List<string> responses = new List<string>();
for (int i = 0; i < count; i++)
responses.Add(prefixText + (char)(i + 65));
return responses.ToArray();
}
}
在aspx文件中拖入一个文本框和一个AutoCompleteExtender控件,aspx源码如下
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
/// 下边这句很重要,引用AjaxControlToolkit.dll
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>
<%@ Register assembly="System.Web.Ajax" namespace="System.Web.UI" tagprefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
/// 下边这句很重要,引用AjaxControlToolkit.dll
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>
<%@ Register assembly="System.Web.Ajax" namespace="System.Web.UI" tagprefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
//下边这就是给页面加入AjaxScriptManager,不然AutoCompleteExtender不能使用
<asp:AjaxScriptManager ID="AjaxScriptManager1" runat="server"></asp:AjaxScriptManager>
<asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>
<asp:AutoCompleteExtender
ID="AutoCompleteExtender1"
runat="server"
ServicePath="WebService.asmx"
ServiceMethod="GetSuggestions"
TargetControlID="TextBox1"
MinimumPrefixLength="2"
CompletionSetCount="10"
CompletionInterval="100">
</asp:AutoCompleteExtender>
</div>
</form>
</body>
</html>
<asp:AjaxScriptManager ID="AjaxScriptManager1" runat="server"></asp:AjaxScriptManager>
<asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>
<asp:AutoCompleteExtender
ID="AutoCompleteExtender1"
runat="server"
ServicePath="WebService.asmx"
ServiceMethod="GetSuggestions"
TargetControlID="TextBox1"
MinimumPrefixLength="2"
CompletionSetCount="10"
CompletionInterval="100">
</asp:AutoCompleteExtender>
</div>
</form>
</body>
</html>
要成功实现搜索补全功能需要在项目中添加AjaxControlToolkit.dll,并在用的得地方引用