<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
<asp:TextBox ID="txtCity" runat="server" />
<asp:AutoCompleteExtender ID="autoCity" runat="server" CompletionSetCount="8" TargetControlID="txtCity" MinimumPrefixLength="1" ServiceMethod="GetCities" ServicePath="AutoCompleteService.asmx"/>
</div>
</form>
<%@ WebService Language="C#" Class="AutoCompleteService" %>
using System;
using System.Collections;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
[Microsoft.Web.Script.Services.ScriptService]
public class AutoCompleteService : System.Web.Services.WebService {
[WebMethod]
public string[] GetCities(string prefixText, int count)
{
string[] autoCompleteWordList = { "Canberra", "Sydney", "Darwin", "Brisbane", "Adelaide", "Hobart", "Melbourne", "Perth" };
Array.Sort(autoCompleteWordList, new CaseInsensitiveComparer());
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[] returnCities = new string[matchingCount];
if (matchingCount > 0)
{
Array.Copy(autoCompleteWordList, index, returnCities, 0, matchingCount);
}
return returnCities;
}
}