(1)开发WebService
以下功能从数据库读取字段的值,存入string[]中,并返回。。。。
文件名:AutoCompleteService_CustName.asmx
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Configuration;
using System.Collections.Generic;
using WebSites;
/// <summary>
/// AutoCompleteService_CustName 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService()]//关键引用
public class AutoCompleteService_CustName : System.Web.Services.WebService {
private static string[] m_autoCompleteWordList = null;
public AutoCompleteService_CustName () {
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod]
public string[] GetWordList(string prefixText, int count)
{
string strSQL = "SELECT FA_CUST_NAME_CHN FROM FA_CUSTOMER ORDER BY FA_CUST_CODE";
string con = "Data Source=DAVID;Initial Catalog=fap;Persist Security Info=True;User ID=sa;Password=sa";
SqlConnection myCn = new SqlConnection(con);
SqlCommand cmd = new SqlCommand(strSQL, myCn);
try
{
myCn.Open();
SqlDataReader myReader = cmd.ExecuteReader();
ArrayList list = new ArrayList(count);
int i = 0;
while (myReader.Read())
{
list.Add(myReader.GetString(0));
i++;
}
if (m_autoCompleteWordList == null)
{
//string[] temp = File.ReadAllLines(Server.MapPath("~/common/word.txt"));
string[] temp = (string[])list.ToArray(typeof(string));
Array.Sort(temp, new CaseInsensitiveComparer());
m_autoCompleteWordList = temp;
}
int index = Array.BinarySearch(m_autoCompleteWordList, prefixText, new CaseInsensitiveComparer());
if (index < 0)
{
index = ~index;
}
int matchingCount;
for (matchingCount = 0; matchingCount < count && index + matchingCount < m_autoCompleteWordList.Length; matchingCount++)
{
if (!m_autoCompleteWordList[index + matchingCount].StartsWith(prefixText, StringComparison.CurrentCultureIgnoreCase))
break;
}
string[] returnValue = new String[matchingCount];
if (matchingCount > 0)
{
Array.Copy(m_autoCompleteWordList, index, returnValue, 0, matchingCount);
}
return returnValue;
}
catch (System.Data.SqlClient.SqlException e)
{
throw new Exception(e.Message);
}
finally
{
myCn.Close();
}
}
}
(2)在页面中添加AutoCompleteExtender控件,使用上面的WebService
Aspx页面代码如下:
<asp:TextBox ID="TextBox1" runat="server" Height="14px" Style="border-right: #6dabe3 1px solid;
border-top: #6dabe3 1px solid; font-size: 11px; border-left: #6dabe3 1px solid;
color: #21325e; line-height: 15px; border-bottom: #6dabe3 1px solid; font- family: Verdana, Tahoma, Arial;background-color: #ffffff" Width="200px"></asp:TextBox>
<cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" CompletionSetCount="20" MinimumPrefixLength="1" ServiceMethod="GetWordList" ServicePath="AutoCompleteService_CustName.asmx"
TargetControlID="TextBox1">
AutoCompleteExtender 属性说明
CompletionSetCount:指定自动完成下拉框最多匹配20个行
MinimumPrefixLength:输入框在输入第一个字符後,就开始启用自动完成功能
ServicePath:调用的WebService类
ServiceMethod:调用的WebService类
中的方法
TargetControlID:启用自动完成的控件ID
注意:本人在开发此功能时候发生过非常重要问题!!!
TextBox的AutoComplete功能为啥会有好多Undefined,而在XML中显示正确了,到aspx中就失败了。
这种现象一个关键问题是
我从数据库中读出的是字符型,比如:Nd00saasda等,这样是没问题。
出现Undefined的页面主要是数据库中读出的都是以数字开头的,比如3458887等(编号)。
这个是Ajax Control ToolKits 的一个Bug,和大家的开发思路没有关系。
解决办法是:下载最新的版本,新的一个版本中解决了这个 Bug。版本 11119。
你可以点击这里进行下载:
http://www.codeplex.com/AtlasControlToolkit/Release/ProjectReleases.aspx?ReleaseId=8513
关于这个问题,社区也进行了解释,望大家注意一下。