ASP.NET AJAX AutoComplete Example Using AutoCompleteExtender Control

ASP.NET AJAX AutoComplete Example Using AutoCompleteExtender Control

by David Hayden ( Florida .NET C# SQL Server Developer ), Filed: ASP.NET 2.0

 

The AutoCompleteExtender Control in the ASP.NET AJAX Toolkit is an awesome way to offer suggestions to users in your ASP.NET Web Applications. It is also very easy to use.

Create an ASP.NET AJAX Website and toss a TextBox and the AutoCompleteExtender Control on the page.

 

AutoCompleteExtender Control

 

Fill out the details of the control to specify the appropriate ServicePath and ServiceMethod of the web service that the AutoCompleteExtender Control will call. In this case I have a web service, called MyAutocompleteService, with a web method, called GetSuggestions. The control will start offering me suggestions once the first character is typed into the TextBox and ask for up to 12 suggestions.

 

<cc1:AutoCompleteExtender

    ID="AutoCompleteExtender1"

    runat="server"

    ServicePath="MyAutocompleteService.asmx"

    ServiceMethod="GetSuggestions"

    TargetControlID="TextBox1"

    MinimumPrefixLength="1"

    CompletionSetCount="12">

</cc1:AutoCompleteExtender>

 

The GetSuggestions WebMethod on the WebService echos back what was typed in an just appends up to 12 characters ( A- L ) to the text typed in. Note the addition of the [ScriptService] Attribute to use the WebService with AJAX.

 

[WebService(Namespace = "http://tempuri.org/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

[ToolboxItem(false)]

[ScriptService]

public class MyAutocompleteService : WebService

{

    [WebMethod]

    public string[] GetSuggestions(string prefixText, int count)

    {

        List<string> responses = new List<string>();

        for (int i = 0; i < count; i++ )

            responses.Add(prefixText + (char)(i + 65));

 

        return responses.ToArray();

    }

}

 

The end result is a nice autcompletion service using ASP.NET AJAX for a richer user interface.

 

AutoCompleteExtender Control in AJAX Control Toolkit

 

News Feed: David Hayden ( Florida .NET C# SQL Server Developer ), Filed: ASP.NET 2.0

 

posted on Monday, October 22, 2007 11:04 PM

Main

News

Green Tea

.NET Development

Enterprise Library

Patterns & Practices