ASP.NET AJAX Control Toolkit 新扩展特性:AutoCompleteExtender

一直期待ATLAS能够提供AutoComplete的扩展特性,终于不负众望,在最新版的ASP.NET AJAX Control Toolkit 已经包含了这个特性:AutoCompleteExtender。使用起来是很简单的。
以下举个股票代码查询的例子,输入部分代码、拼音缩写或者汉字,便能得到全部的提示。

1 添加一个TextBox
<asp:TextBox runat="server" ID="txtStockKeyName" Width="300" autocomplete="off" />

2 添加AutoCompleteExtender,并设置参数
<cc1:AutoCompleteExtender
                runat="server"
                ID="autoComplete1"
                TargetControlID="txtStockKeyName"
                ServicePath="StockCode.asmx"
                ServiceMethod="GetStockList"
                MinimumPrefixLength="1"
                CompletionInterval="1000"
                EnableCaching="true"
                CompletionSetCount="12"
                 />


参数说明:
TargetControlID : 输入查询关键字的文本框
ServicePath         :   对应后台的Webservice的地址
ServiceMethod   :  对应后台的Webservice的方法名
MinimumPrefixLength :查询关键字长度产生自动完成的最短长度
CompletionInterval :查询结果显示间隔时间(毫秒)
EnableCaching    : 启用Cache
CompletionSetCount :显示查询结果的条数

3 根据查询关键字返回查询结果的Webservice方法
 

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

/// <summary>
/// StockCode 的摘要说明
/// </summary>

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo 
= WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class StockCode : System.Web.Services.WebService {

    
public StockCode () {

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


    [WebMethod]
    
public string[] GetStockList(string prefixText, int count)
    
{
        
if (count == 0)
        
{
            count 
= 10;
        }


        Random random 
= new Random();
        List
<string> items = new List<string>(count);

        
for (int i = 0; i < stockID.Length; i++)
        
{
            
if (   
                     (stockID[i].IndexOf(prefixText) 
>= 0
                  
|| (stockName[i].IndexOf(prefixText) >= 0)
                  
|| (stockPY[i].ToUpper().IndexOf(prefixText.ToUpper()) >= 0)
                )
            
{
                items.Add(stockID[i] 
+ ":" + stockName[i]);
            }

        }


        
return items.ToArray();
    }


    
//股票代码
    string[] stockID = "610001""610002""611003""611004" };
    
//股票拼音简称
    string[] stockPY = "xg""ng""xj""xc" };
    
//股票名称
    string[] stockName = "西瓜""南瓜""香蕉""香肠" };
    
}


效果图:




示例代码:
/Files/heekui/StockID.rar

运行环境:
VS2005 + ASP.NET AJAX V 1.0 + ASP.NET AJAX Control Toolkit

 

posted on 2007-02-15 00:52  网际浪人  阅读(2675)  评论(12编辑  收藏  举报