现在的位置: 首页 > 综合 > 正文

.net3.5 和vs2008中Ajax控件的使用–AutoComplete(自动完成控件)的使用

2013年01月27日 ⁄ 综合 ⁄ 共 1675字 ⁄ 字号 评论关闭

一、属性

1、TargetControlID:指定要实现提示功能的控件。   

2、ServicePath:WebService的路径,提取数据的方法是写在一个WebService中的。    

3、ServeiceMethod:写在WebService中的用于提取数据的方法的名字。    

4、MinimumPrefixLength:用来设置用户输入多少字母才出现提示效果。    

5、CompletionSetCount:设置提示数据的行数。    

6、CompletionInterval:从服务器获取书的时间间隔,单位是毫秒。

二、使用实例说明

a、新建Ajax Web 窗体,将其命名为AutoComplete.aspx。

b、在窗体上拖放一个TextBox控件,并为该控件添加AutoComplete程序扩展

添加完扩展后,源代码页面如下:

<cc1:AutoCompleteExtender ID="TextBox1_AutoCompleteExtender" runat="server" 

         Enabled="True"  TargetControlID="TextBox1" ServicePath="AutoComplete.asmx" 

                ServiceMethod="GetCompletionList"

                MinimumPrefixLength="0" 

                CompletionInterval="500"

                EnableCaching="true"

                CompletionSetCount="20">

    </cc1:AutoCompleteExtender>

在这里大家可以看到我们使用了一个web服务页面AutoComplete.asmx来完成提取数据的方法GetCompletionList的。页面代码如下:

using System;

using System.Collections;

using System.Linq;

using System.Web;

using System.Web.Services;

using System.Web.Services.Protocols;

using System.Xml.Linq;

using System.Collections.Generic;



/// <summary>

///AutoComplete 的摘要说明

/// </summary>

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

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

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

[System.Web.Script.Services.ScriptService]

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



    public AutoComplete () {



        //如果使用设计的组件,请取消注释以下行 

        //InitializeComponent(); 

    }



    [WebMethod]

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

    {

        if (count == 0)

        {

            count = 10;

        }



        if (prefixText.Equals("xyz"))

        {

            return new string[0];

        }



        Random random = new Random();

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

        for (int i = 0; i < count; i++)

        {

            char c1 = (char)random.Next(65, 90);

            char c2 = (char)random.Next(97, 122);

            char c3 = (char)random.Next(97, 122);



            items.Add(prefixText + c1 + c2 + c3);

        }



        return items.ToArray();

    }

    

}

c、这里整个功能就完成了,效果如下:

抱歉!评论已关闭.