简介
AutoComplete控件就是在用户在文本框输入前几个字母或是汉字的时候,该控件就能从存放数据的文或是数据库里将所有以这些字母开头的数据提示给用户,供用户选择,提供方便。
重要属性
1.TargetControlID:指定要实现提示功能的控件;
2.ServicePath:WebService的路径,提取数据的方法是写在一个WebService中的;
3.ServeiceMethod:写在WebService中的用于提取数据的方法的名字;
4.MinimumPrefixLength:用来设置用户输入多少字母才出现提示效果;
5.CompletionSetCount:设置提示数据的行数;
6.CompletionInterval:从服务器获取书的时间间隔,单位是毫秒。
新建一个webservices文件,输入代码:
InBlock.gifusing System;
InBlock.gifusing System.Web;
InBlock.gifusing System.Collections;
InBlock.gifusing System.Collections.Generic;
InBlock.gifusing System.Web.Services;
InBlock.gifusing System.Web.Services.Protocols;
InBlock.gifusing System.IO;
/// <summary>
/// AutoComplete 的摘要说明
/// <summary>
InBlock.gif[WebService(Namespace = "http://tempuri.org/")]
InBlock.gif[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
InBlock.gif[System.Web.Script.Services.ScriptService]
InBlock.gifpublic class AutoComplete : System.Web.Services.WebService
InBlock.gif{
InBlock.gif        
InBlock.gif        /// <summary>
InBlock.gif        /// 获取数据的方法GetSuggestions
InBlock.gif        /// <summary>
InBlock.gif        //定义静态数组用于保存获取的数据
InBlock.gif        private static string[] autoCompleteWordList = null;
InBlock.gif        [WebMethod]
InBlock.gif        public string[] GetSuggestions(string prefixText, int count)
InBlock.gif        {
InBlock.gif                List<string> responses = new List<string>();
InBlock.gif                for (int i = 0; i < count; i++)
InBlock.gif                        responses.Add(prefixText + (char)(i + 65));
InBlock.gif
                return responses.ToArray();
InBlock.gif        }
InBlock.gif}
在aspx文件中拖入一个文本框和一个AutoCompleteExtender控件,aspx源码如下
<%@ Page Language="C#" AutoEventWireup="true"    CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ 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>
        
                <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>
.cs源码
InBlock.gifusing System;
InBlock.gifusing System.Collections.Generic;
InBlock.gifusing System.Linq;
InBlock.gifusing System.Web;
InBlock.gifusing System.Web.UI;
InBlock.gifusing System.Web.UI.WebControls;
InBlock.gif
public partial class _Default : System.Web.UI.Page
InBlock.gif{
InBlock.gif        protected void Page_Load(object sender, EventArgs e)
InBlock.gif        {
InBlock.gif
        }
InBlock.gif}