当前位置:编程学习 > C#/ASP.NET >>

ajax 框架autocompleteextender实现自动完成功能

ajax 框架autocompleteextender实现自动完成功能

需要一个WebService 我也懒得改名子,就直接叫WebService.asmx; 为什么要用WebService? 其实我也不太清楚,只知道AutoCompleteExtender需要三个最为关键的属性:

ServicePath="WebService.asmx" 

ServiceMethod="GetWordList"

TargetControlID="txtText"

 

如果知道这三个属性的话,也许就清楚为什么要用WebService了,ServicePath:就是WebService的路径,ServiceMethod:WebService中的方法名称,TargetControlID就是要对哪个控件实现自动完成效果(说的有点不清楚,但明白是什么意思就行了);

代码如下:

View Code using System;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
using CommonUtility;


namespace GridView入库单管理
{
/// <summary>
/// WebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{
[WebMethod]
public String[] GetWordList(string prefixText, int count)
{
string sql = string.Format("select top {0} * from InBill where saleName like '%" + @prefixText + "%'", @count);

SqlParameter[] paras = new SqlParameter[]
{
new SqlParameter("@prefixText",prefixText),
new SqlParameter("@count",count)
};

DataTable table = SQLHelper.GetDateSet(sql, CommandType.Text, paras);
string[] arr = new string[table.Rows.Count];
if (table != null)
{
for (int i = 0; i < table.Rows.Count; i++)
{
arr[i] = table.Rows[i]["saleName"].ToString();
}
}
return arr;
}
}
}

 

SQL语句:在声名方法的时候,Count就是为了这个时候用的,AutoCompleteExtender 中加上CompletionSetCount="5" 的时候, 就有用了,它是什么意思? 他就是自动完成的时候,显示多少条数据用的,如果不写,默认是10;也就是说,下拉列表中会出现10条数据;如果定义完以后,在这里就可以将Count传进去了;

string sql = string.Format("select top {0} * from InBill where saleName like '%" + @prefixText + "%'", @count);

 

string[] arr = new string[table.Rows.Count]; //定义一个字符串类型的数组,让他的长度等于我们查出来的table的行数;紧接着就要遍历table,把每行的数据都填充到数组中去;

 

arr[i] = table.Rows[i]["saleName"].ToString(); saleName是数据库教程中的字段名,你这里绑定的是哪个字段,自动完成的时候就会显示哪个字段的值;

 

Aspx页面代码片段:

<asp教程:ScriptManager ID="ScriptManager1" runat="server" />
<asp:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" MinimumPrefixLength="1"
CompletionInterval="500" EnableCaching="false" ServiceMethod="GetWordList" ServicePath="WebService.asmx"
TargetControlID="txtText" CompletionSetCount="5" />
<asp:TextBox ID="txtText" runat="server"></asp:TextBox>

 

首先需要一个ScriptManager,这个是必须的,下面解释一下AutoCompleteExtender中各个属性的意思;

 

MinimumPrefixLength : 就是最小输入几个字符的时候弹出自动完成;

CompletionInterval:自动完成时间间隔;

EnableCaching:是否启用缓存;

ServiceMethod:WebService中的方法名称;

ServicePath:WebService路径;

TargetControlID:绑定的控件;

CompletionSetCount:显示自动完成的行数;

一个详细的实例

功能:

         可以辅助TextBox控件自动输入,如在google中搜索时。

属性:

         TargetControlID:指定将被辅助完成自动输入的控件ID,这里的控件只能是TextBox;

  ServicePath:指出提供服务的WEB服务路径,若不指出则ServiceMethod表示本页面对应的方法名;

  ServiceMethod:指出提供服务的方法名;

  MinimumPrefixLength:指出开始提供提示服务时,TextBox控件应有的最小字符数,默认为3;

  CompletionSetCount:显示的条数,默认为10;

  EnableCaching:是否在客户端缓存数据,默认为true;

  CompletionInterval:从服务器读取数据的时间间隔,默认为1000,单位:毫秒。

代码实例:

 ASPX页面代码:

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>AutoComplete server control</title>

</head>

<body>

    <form id="form1" runat="server">

        <asp:ScriptManager runat="server" ID="ScriptManager1" />

        <cc1:AutoCompleteExtender

            ID="AutoCompleteExtender1"

            runat="server"

            ServicePath="AutoComplete.asmx"           

            TargetControlID="TextBox1"            

            ServiceMethod="GetWordList"

            MinimumPrefixLength="1"

            EnableCaching ="true"

            CompletionSetCount="12"

            CompletionInterval="1000">                      

        </cc1:AutoCompleteExtender>

           

        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 

       

 

    </form>  

</body>

</html>

编写相应的webservices

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

 

    public AutoComplete () {

 

        //Uncomment the following line if using designed components

        //InitializeComponent();

    }

 

    private static string[] autoCompleteWordList = null;

 

    [WebMethod]

    public String[] GetWordList(string prefixText, int count)

    {

        if (autoCompleteWordList == null)

        {

            string[] temp = File.ReadAllLines(Server.MapPath("~/App_Data/words.txt"));

            Array.Sort(temp, new CaseInsensitiveComparer());

            autoCompleteWordList = temp;

        }

 

        int index = Array.BinarySearch(autoCompleteWordList, prefixText,

          new CaseInsensitiveComparer());

        if (index < 0)

        {

            index = ~index;

        }

 

        int matchingCount;

        for (matchingCount = 0;

             matchingCount < count && index + matchingCount <

             autoCompleteWordList.Length;

             matchingCount++)

        {

            if (!autoCompleteWordList[index +

              matchingCount].StartsWith(prefixText,

              StringComparison.CurrentCultureIgnoreCase))

            {

                break;

            }

        }

 

        String[] returnValue = new string[matchingCount];

        if (matchingCount > 0)

        {

            Array.Copy(autoCompleteWordList, index, returnValue, 0,

              matchingCount);

        }

        return returnValue;

    }

}

 

  在这里需要注意以下几点:

   1.由于该WEB服务是为Ajax框架提供服务的,因此在类声明之前得加上属性声明:

     [System.Web.Script.Services.ScriptService]

   2.特别需要注意的是GetTextString这个方法。凡是为AutoCompleteExtender控件提供服务的方法都必需完全满足以下三个条件:

     A.方法的返回类型必需为:string [];

     B.方法的传入参数类型必需为:string  ,   int;

     C.两个传入参数名必需为:prefixText  ,  count。

在App_Data下添加words.txt。

access control list (ACL)

 

ADO.NET

 

aggregate event

 

alpha channel

 

anchoring

 

antialiasing

 

application base

 

application domain (AppDomain)

 

application manifest

 

application state

 

ASP.NET

 

ASP.NET application services database

 

ASP.NET mobile controls

 

ASP.NET mobile Web Forms

 

ASP.NET page

 

ASP.NET server control

 

ASP.NET Web application

 

补充:asp.net教程,.Net开发 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,