Click here to Skip to main content
15,881,882 members
Articles / Web Development / ASP.NET

AutoCompleteExtender with Data from ASPX Code Behind

Rate me:
Please Sign up or sign in to vote.
4.67/5 (2 votes)
9 Jan 2011CPOL4 min read 36.3K   2   6
AutoCompleteExtender with data from ASPX code behind

The auto complete functionality is a useful aid where suggestions are displayed when you type into a textbox. You would have come across this in many sites. Looking at how to implement this in an ASP.NET application using the AJAX control toolkit, it was not a complex task, as the great video tutorial here demonstrates. But the video (and many other articles on the web) showed how to use the auto complete extender hooked up to a ASMX file. Almost all the articles described how AJAX retrieves the auto suggestion text via a web service and displays them when the user types into the text box. But my need was to get the suggested text from the code behind of the ASPX file where the textbox was displayed. I needed the AJAX control to communicate with a method in the ASPX code behind, instead of a service. Documented in the following sections is how I got it working.

In case you don’t have the AJAX control toolkit downloaded and integrated with Visual Studio, the video here shows how to obtain the toolkit, and set it up in the development environment. I used Visual Web developer 2010 express for the example given here. Opening up the IDE, create an ASP.NET web project and reference the AJAX control toolkit DLL. You can also create a new tab in the toolbox and add the controls to it by selecting the DLL (the steps to do this are in the same video link). Drag a textbox to the default.aspx page, this will be the textbox where the user will type in and get the autocomplete suggestions.

Switching to source view in default.aspx, drag a ToolkitScriptManager from the AJAX control toolkit tab in the toolbox into the source window:

Image 1

You need a script manager when using the AJAX control tools. The script manager takes care of generating the code to communicate with the back end using AJAX without doing a post back of the page. ASP.NET AJAX extensions already provide a script manager, but the ToolkitScriptManager inherits from this previous component, and is better in performance due to script combining.

Next, drag the AutoCompleteExtender control onto the source window. This will be the AJAX component that specifies which control in the ASPX page gets the auto complete functionality and how:

Image 2

Some properties in the AutoCompleteExtender have to specified in order for the auto complete functionality to work, and these are shown in the below screenshot:

Image 3

TargetControlID is the ID of the textbox to which the AutoCompleteExtender is connected. ServicePath and ServiceMethod are the web service path and web method respectively. But as we are retrieving auto complete suggestions from our default.aspx.cs code behind, the ServicePath is left empty, and the ServiceMethodGetNames’ is the name of the static method in the code behind that will return the array of strings triggered by the AJAX request.

The code behind file, default.aspx.cs will contain the method that will return the array of strings to the AJAX control in the ASPX page. The method has to be declared static, and should be decorated with the ‘WebMethod’ and ‘ScriptMethod’ attributes. WebMethod is used to specify that the method is part of a service, and ScriptMethod is used to identify a method that AJAX can invoke. The complete code in the default.aspx.cs is given below:

Image 4

Some specifics to note:

  • The ServiceMethod can have any name, but it should be declared static, and it should be decorated with the WebMethod and ScriptMethod attributes.
  • The parameters of the method should be ‘prefixText’ and ‘count’, of type string and int, respectively. They are case sensitive and cannot be changed to any other variable name. prefixText is the text we enter into the textbox, sent over by AJAX, and count is the number of characters typed in. They can be used in many ways, specially when retrieving string values from a database.
  • The method should return an array of strings. There are workarounds to return collections and key-value pairs, but they are out of the scope of this post.

When you debug the web application, you should see the auto complete functionality that you have wired up to your textbox, when you type in some text:

Image 5

I had to type a minimum of three characters before the auto complete kicked in, but this minimum number of characters can be set in the AutoCompleteExtender control, using the ‘MinimumPrefixLength’ property. This basically covers the essentials needed for passing data to the auto complete AJAX control using a method from the ASPX code behind file, instead of a Web service or WCF service.

Image 6 Image 7 Image 8 Image 9 Image 10 Image 11 Image 12 Image 13

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead Exilesoft
Sri Lanka Sri Lanka
Mark is a Technical Lead at Exilesoft, whose passion lies in coding, mentoring, and fueling technical innovation. In the past, he has worked as a developer for a product engineering company, an ERP/Technical Consultant in a Fortune 500 conglomerate, and also as a senior engineer for a startup in the manufacturing and design space. His current areas of research revolve around Enterprise Architecture, Big Data, NoSQL Technology, and Machine Learning.

In his spare time, Mark experiments with (computer) game design/development, operating system internals, and compiler design. He also discusses and blogs about various topics surrounding software development, computer science, game programming, and mathematics, which can be read at markfaction.wordpress.com. Feel free to email or message him anytime and strike up a conversation.

Comments and Discussions

 
QuestionMulti AutoCompleteExtender Pin
hanx.xcb18-Sep-12 6:41
hanx.xcb18-Sep-12 6:41 
AnswerRe: Multi AutoCompleteExtender Pin
Mark Vinod Sinnathamby18-Sep-12 7:10
professionalMark Vinod Sinnathamby18-Sep-12 7:10 
GeneralRe: Multi AutoCompleteExtender Pin
hanx.xcb18-Sep-12 7:25
hanx.xcb18-Sep-12 7:25 
GeneralMy vote of 4 Pin
Tapan dubey7-Jul-11 23:52
Tapan dubey7-Jul-11 23:52 
GeneralGood one!! Pin
meeram3959-Jan-11 20:43
meeram3959-Jan-11 20:43 
GeneralRe: Good one!! Pin
Mark Vinod Sinnathamby10-Jan-11 5:36
professionalMark Vinod Sinnathamby10-Jan-11 5:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.