本文详细介绍AjaxControlToolkit中AutoCompleteExtender自动完成扩展控件的配置方法。涵盖TargetControlID、ServicePath等关键属性设置,以及如何通过Web Service接口实现数据检索。包含完整的C#代码示例,演示如何从数据库或内存中获取数据并返回给前端TextBox控件,实现类似Google搜索的下拉提示功能。
AutoCompleteExtender 简介与核心属性
AutoCompleteExtender 是 AjaxControlToolkit 提供的自动完成扩展控件,通常与 TextBox 配合使用。其功能类似于在 Google 搜索框中输入关键词时,下方自动弹出包含相关项目的下拉列表。
该扩展控件必须配合 Web Service 使用,因此涉及 Web Service 的相关配置。本文仅简要介绍其用法,待熟悉后再深入探讨 Web Service 的具体内容。
首先介绍 AutoCompleteExtender 的几个关键属性:
a. TargetControlID:这是所有 AjaxControlToolkit 扩展控件共有的属性,用于指定扩展目标控件的 ID。
b. CompletionSetCount:设置显示在下拉列表中的结果条数,默认值为 10。
c. MinimumPrefixTextLength:设置用户输入多少个字符后触发调用 Web Service 中的方法以显示下拉列表。
d. ServicePath:设置需要调用的 Web Service 的路径。
e. ServiceMethod:设置需要调用的 Web Service 中的具体方法(函数)名称。
f. EnableCaching:是否启用客户端缓存数据,默认值为 true。
g. CompletionInterval:从服务器读取数据的时间间隔,默认值为 1000,单位为毫秒。
注意:如果使用可视化控件设置属性,a 属性在 AutoCompleteExtender 中设置;其他属性则在设置了 TargetControlId 后,在相应的 TargetControl 的 Extenders 属性中设置。如果习惯手写代码,则直接在 AutoCompleteExtender 的代码属性中设置。
Web Service 实现与页面配置示例
示例步骤:1. 新建一个页面,添加 ScriptManager 控件、一个 TextBox 控件和一个 AutoCompleteExtender 控件。
2. 新建一个 Web Service,并添加一个标记为 [WebMethod] 的方法。
[WebMethod]
代码如下:
public string[] GetString(string prefixText, int count){
System.Collections.Generic.List<string> list = new System.Collections.Generic.List<string>(count);
System.Data.DataSet ds = new System.Data.DataSet();
//这里是我在数据库中取数据的代码 其中SqlHelper类是项目中的取数据基类
//string strSql = string.Format("SELECT TOP {0} NAME FROM CengWei WHERE NAME LIKE '{1}%' ORDER BY NAME",count,prefixText);
//ds = SqlHelper.Query(strSql);
//for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
//{
// list.Add(ds.Tables[0].Rows[i][0].ToString());
//}
for (int i = 0; i < count; i++)
{
list.Add(prefixText+i.ToString());
}
return list.ToArray();
} 其中:必须在webService的类上面添加
[System.Web.Script.Services.ScriptService]
示例代码:webService是在数据库中的一个字段中取数据
页面代码:
代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test2.aspx.cs" Inherits="test2" %>
<%@ Register Assembly="CrystalDecisions.Web, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"
Namespace="CrystalDecisions.Web" TagPrefix="CR" %>
<%@ 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>DropDownExtender简单练习</title>
<link href="/aspnet_client/System_Web/2_0_50727/CrystalReportWebFormViewer3/css/default.css"
rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" EnableScriptGlobalization="True" EnableScriptLocalization="True">
</asp:ScriptManager>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
<cc1:AutoCompleteExtender ID="AutoCompleteExtender2" runat="server" MinimumPrefixLength="1"
ServiceMethod="GetString" ServicePath="AutoComplete.asmx" TargetControlID="TextBox2">
</cc1:AutoCompleteExtender>
</form>
</body>
</html> webService代码:
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
/// <summary>
/// AutoComplete 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//下面是必须的,否则功能无法实现
[System.Web.Script.Services.ScriptService]
public class AutoComplete : System.Web.Services.WebService {
public AutoComplete () {
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
[WebMethod]
public string[] GetString(string prefixText, int count){
System.Collections.Generic.List<string> list = new System.Collections.Generic.List<string>(count);
System.Data.DataSet ds = new System.Data.DataSet();
//这里是我在数据库中取数据的代码 其中SqlHelper类是项目中的取数据基类
//string strSql = string.Format("SELECT TOP {0} NAME FROM CengWei WHERE NAME LIKE '{1}%' ORDER BY NAME",count,prefixText);
//ds = SqlHelper.Query(strSql);
//for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
//{
// list.Add(ds.Tables[0].Rows[i][0].ToString());
//}
for (int i = 0; i < count; i++)
{
list.Add(prefixText+i.ToString());
}
return list.ToArray();
}
}
有哪里不对的地方还请大家指教