AutoCompleteProperties的属性包括 属性名称 属性描述 备注
TargetControlID 指定要控制的控件的ID 一般为TextBox的ID
ServicePath 处理智能选择列表的Web Services路径
ServiceMethod 处理智能选择列表的网络服务服务 该方法一般包含两个参数(string prefixText, int count)
Enabled 是否可用
MinimumPrefixLength 最小前缀的长度大小 当输入长度达到最小的时候,便提供智能选择
一、查看web.config文件中是否存在如下配置(倘若没有请添加):
<system.web>
<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>
</httpHandlers>
<system.web>
}
}
在这里需要注意以下几点: 1.由于该WEB服务是为Ajax框架提供服务的,因此在类声明之前得加上属性声明:
[System.Web.Script.Services.ScriptService]
2.特别需要注意的是GetCompletionList这个方法。凡是为AutoCompleteExtender控件提供服务的方法都必需完全满足以下三个条件:
A.方法的返回类型必需为:string [];
B.方法的传入参数类型必需为:string , int;
C.两个传入参数名必需为:prefixText , count。
二、代码如下:
2_SimpleList_AutoComplete_DragandDrop.aspx文件
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="2_SimpleList_AutoComplete_DragandDrop.aspx.cs"
Inherits="_2_SimpleList_AutoComplete_DragandDrop" %>
<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI" TagPrefix="asp" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!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>Untitled Page</title>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="WebServiceSearchName.asmx" />
</Services>
</asp:ScriptManager>
<asp:TextBox runat="server" ID="TextBoxInput" Width="300" autocomplete="off" />
<asp:AutoCompleteExtender runat="server" ID="AutoCompleteExtender1" TargetControlID="TextBoxInput"
ServicePath="webservicesearchname.asmx" ServiceMethod="GetCompletionList" MinimumPrefixLength="1"
CompletionInterval="100" EnableCaching="true" CompletionSetCount="12" />
</div>
</form>
</body>
</html>
添加服务WebServiceSearchName, 且WebServiceSearchName.cs文件在App_Code文件夹下,其实内容如下:
using System;
using System.Web.Services;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
/**//// <summary>
/// Summary description for WebServiceSearchName
/// </summary>
[System.Web.Script.Services.ScriptService]
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebServiceSearchName : System.Web.Services.WebService ...{
public WebServiceSearchName () ...{
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string[] GetCompletionList(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 < 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();