AJAX AutoComplete的用法

本文介绍如何在ASP.NET应用中实现自动完成功能,通过AJAX Control Toolkit中的AutoCompleteExtender控件与Web Services相结合,为用户提供即时搜索建议。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

调用页:
//在页面顶部注册控件
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
//TextBox控件
<asp:TextBox ID="txtTo" runat="server" Width="500px" AutoPostBack="True"></asp:TextBox>
<cc1:AutoCompleteExtender ID="AutoCompleteExtenderSearch" 
	MinimumPrefixLength="1"//这是设置输入几个字符才开始调用搜索
	CompletionInterval="10" 
	runat="server"
	EnableCaching="true"
	ServicePath="AutoCompleteSend.asmx"//这是Web Services的路径
	ServiceMethod="GetToAddress"//这是处理搜索的Web Services下的一个方法
	TargetControlID="txtTo">//这是对应的TextBox
</cc1:AutoCompleteExtender>
被调用的Web Services:
using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Services;

using System.Web.Script.Services;

using System.Web.Services.Protocols;

using System.Xml.Linq;



namespace MySystem.Email

{

    /// <summary>

    /// AutoCompleteSend 的摘要说明

    /// </summary>

    [WebService(Namespace = "http://tempuri.org/")]

    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

    [ToolboxItem(false)]

    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。

    [System.Web.Script.Services.ScriptService]//这个很重要,一定要有

    public class AutoCompleteSend : System.Web.Services.WebService

    {



        [WebMethod(EnableSession=true)]//这是打开支持Session的

        [ScriptMethod]//这个很重要,一定要有

        public string[] GetToAddress(string prefixText, int count)

        {

            //这里我就不详细写了,只要返回一个string[]就可以了

            return new string[]{"第一行","第二行"};

        }

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值