ASP.net+AJAX智能匹配检索(自动完成)

来源:互联网 发布:ug自动编程的步骤 编辑:程序博客网 时间:2024/05/01 23:20

【转】http://hi.baidu.com/%D6%EC%CE%C4%BC%D2/blog/item/1a86cf4e83981acdd0c86a78.html

 

该功能能够根据用户的输入显示一个在线提示列表。因此,用户只要输入部分关键字,就能够从在线提示列表中选择所需要的关键字。典型的效果如图所示。

如果使用ASP.NET和JavaScript技术实现智能匹配检索这一功能,是非常复杂且比较烦琐的事情。ASP.NET AJAX Control Toolkit中的AutoCompleteExtender控件能够实现功能。声明AutoCompleteExtender扩展器控件的语法类似如下:

<ajaxToolkit:AutoCompleteExtender

    runat="server" ID="ace"

    TargetControlID="TextBox控件"

    ServiceMethod="获取建议的方法的名称"

    ServicePath="获取建议的Web服务"

    MinimumPrefixLength="2"

    CompletionInterval="1000"

    EnableCaching="true"

    CompletionSetCount="20"

    CompletionListCssClass="提示列表的样式"

    CompletionListItemCssClass="未选择项的样式"

    CompletionListHighlightedItemCssClass="选择项的样式"

    DelimiterCharacters=";, :">

    <Animations>

        <OnShow> ... </OnShow>

        <OnHide> ... </OnHide>

    </Animations>

</ajaxToolkit:AutoCompleteExtender>

另外,AutoCompleteExtender控件包含多个常用属性,如TargetControlID、MinimumPrefixLength、ServiceMethod、ServicePath、ContextKey等。具体说明如表2.5所示。

表2.5 AutoCompleteExtender控件的常用属性及其说明

   

   

TargetControlID

使用该控件的ASP.NET服务器端控件的ID

MinimumPrefixLength

获取建议文本的最小字符数量

CompletionInterval

获取建议文本之前等待的时间

EnableCaching

是否使用缓存

CompletionSetCount

建议文本的数量

CompletionListCssClass

建议列表的样式

CompletionListItemCssClass

未被选择的每一项建议文本的样式

CompletionListHighlightedItemCssClass

被选择的建议文本的样式

DelimiterCharacters

分隔字符集合

FirstRowSelected

指定建议文本中的第一项是否被选择

Animations

建议文本的动画

OnShow

显示建议文本时的动画

OnHide

隐藏建议文本时的动画

ServiceMethod

Web服务方法的名称

ServicePath

Web服务的路径(相对路径)

ContextKey

设置Web服务方法的prefixText参数的值

UseContextKey

指定是否使用ContextKey参数

MinimumPrefixLength属性指定一个整数值,当用户输入内容的长度大于或等于该值时,AutoCompleteExtender控件将为输入框显示在线提示列表。CompletionInterval属性设置显示在线提示列表之前等待的时间。CompletionSetCount属性指定在线提示列表一次返回建议的最大数量。

ServicePath属性指定获取在线建议的Web服务;ServiceMethod属性指定获取在线建议的Web服务中方法的名称,该方法必须满足以下代码实例中的签名。

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

[System.Web.Script.Services.ScriptService()]

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

{

    ...

}

ServiceMethod属性指定的方法必须满足以下条件:

[System.Web.Script.Services.ScriptService()] 是WebService专门为AJAX专门订制的特性。在ASP.NET AJAX中调用WebService时必须添加的特性。

— 参数列表必须为“string prefixText,int count”。其中,prefixText参数的值等于ContextKey属性的值,count参数的值等于CompletionSetCount属性的值。

— 方法的返回类型必须为“string[]”,且使用“public”修饰。

— 必须为方法添加“System.Web.Script.Services.ScriptMethod”属性,使得脚本能够调用该方法。

注意:ServiceMethod属性指定的方法的名称可以随意命名,没有硬性规定。

 

 

由于WebService的SOAP协议对泛型序列化的支持不够好,客户端只能默认处理简单的泛性,如List<int>,当泛性比较复杂时,如List<string>,处理时经常转换为string[]。

定义AjaxService Web服务的程序代码如下。

///引入命名空间

using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

[System.Web.Script.Services.ScriptService()]   //添加脚本服务

public class WebService : System.Web.Services.WebService
{

    public WebService()
    {

        //如果使用设计的组件,请取消注释以下行
        //InitializeComponent();
    }


    [WebMethod]
    
    public string[] HotBooks(string prefixText, int count)
    {
        return BookManage.GetHotBooks(prefixText, count);
    }

}

原创粉丝点击