文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>ASP.NET 自动完成 AutoCompleteExtender

ASP.NET 自动完成 AutoCompleteExtender

时间:2010-09-25  来源:Simcoder

要实现的功能:

1.使用VS2008新建一个网站(VS2005新建AJAX网站)

然后再默认Default.aspx页面添加ToolkitScriptManager或ScriptManager控件,然后添加文本框和自动完成控件AutoCompleteExtender,文本框的属性记得设置AutoPostBack为true(自动回发).

代码 <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server" 
            style="width: 128px" Width="400px" AutoPostBack="True" ></asp:TextBox>
        <cc1:AutoCompleteExtender ID="TextBox1_AutoCompleteExtender" runat="server" 
            TargetControlID="TextBox1" ServicePath="WebService.asmx" CompletionSetCount="10" MinimumPrefixLength="1" ServiceMethod="GetTextString">
        </cc1:AutoCompleteExtender>
        <cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
        </cc1:ToolkitScriptManager>
    </div>
    </form>
</body>
</html>

 

 2.自动完成控件属性:

   AutoCompleteExtender控件的属性:
   (1).TargetControlID:指定将被辅助完成自动输入的控件ID,这里的控件只能是TextBox;
   (2).ServicePath:指出提供服务的WEB服务路径,若不指出则ServiceMethod表示本页面对应的方法名;
   (3).ServiceMethod:指出提供服务的方法名;
   (4).MinimumPrefixLength:指出开始提供提示服务时,TextBox控件应有的最小字符数,默认为3;
   (5).CompletionSetCount:显示的条数,默认为10;
   (6).EnableCaching:是否在客户端缓存数据,默认为true;
   (7).CompletionInterval:从服务器读取数据的时间间隔,默认为1000,单位:毫秒。

3.新建WebService.asmx Web服务

webservice.asmx是web服务的页面文件,代码文件默认放置在app_code中(webservice.cs) 代码如下:

 

代码 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
using System.Data;
using System.Collections;

/// <summary>
///WebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 
[System.Web.Script.Services.ScriptService] //不要遗忘掉了取消注释
public class WebService : System.Web.Services.WebService {

    public WebService()
    {

        //如果使用设计的组件,请取消注释以下行 
        //InitializeComponent(); 
    }
    private string[] autoCompleteWordList = null;//保存获取的内容
    [WebMethod]
    public string[] GetTextString(string prefixText, int count)
        //string prefixText, int count 两个参数必须原封不动照写,包括大小写也是一样 返回参数只能是字符串数组
        //prefixText表示用户输入的前缀,count表示返回的个数
    {
        if (string.IsNullOrEmpty(prefixText) == true)
            return null;
        if (autoCompleteWordList == null)
        {
            SqlConnection connection = new SqlConnection("server=.;database=buty_DB;uid=sa;pwd=123456");
            connection.Open();
            SqlDataAdapter mydata = new SqlDataAdapter("select pro_CompanyName from dbo.buy_CompanyInfo", connection);
            DataSet ds = new DataSet();
            mydata.Fill(ds);
            string[] temp = new string[ds.Tables[0].Rows.Count];//临时数组
            int i = 0;
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                temp[i] = dr["pro_CompanyName"].ToString();
                i++;
            }
            Array.Sort(temp, new CaseInsensitiveComparer());
            autoCompleteWordList = temp;//临时数组赋值给刚刚前面申明的数组
            if (connection.State == ConnectionState.Open)
                connection.Close();
        }
        //定位二叉树起点
        int index = Array.BinarySearch(autoCompleteWordList, prefixText, new CaseInsensitiveComparer());
        if (index < 0)
        {
            index = ~index;//修复起点
        }
        //搜索符合条件的数据
        int matchCount = 0;
        for (matchCount = 0; matchCount < count && matchCount + index < autoCompleteWordList.Length; matchCount++)
        {
            if (autoCompleteWordList[index + matchCount].StartsWith(prefixText, StringComparison.CurrentCultureIgnoreCase) == false)
            {
                break;
            }
        }
        //处理搜索结果
        string[] matchResultList = new string[matchCount];
        if (matchCount > 0)
        {
            Array.Copy(autoCompleteWordList, index, matchResultList, 0, matchCount);
        }
        return matchResultList;
    }
}

 

 

注意:  1.由于该WEB服务是为Ajax框架提供服务的,因此在类声明之前得加上属性声明:
     [System.Web.Script.Services.ScriptService]
     2.特别需要注意的是GetTextString这个方法。凡是为AutoCompleteExtender控件提供服务的方法都必需完全满足以下三个条件:
     A.方法的返回类型必需为:string [];
     B.方法的传入参数类型必需为:string  ,   int;
     C.两个传入参数名必需为:prefixText  ,  count。

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载