Click here to Skip to main content
15,887,214 members
Articles / Web Development / HTML
Article

AJAX AutoComplete/AutoSuggest TextBox

Rate me:
Please Sign up or sign in to vote.
4.86/5 (51 votes)
2 Oct 20074 min read 881.2K   7K   147   220
An article on AJAX AutoSuggest control
Sample Image - maximum width is 600 pixels

Introduction

This is an AJAX AutoSuggest ASP.NET textbox I've created a while ago. It is built on top of the Anthem.NET library.

Background

When building enterprise web applications, we often need to let the user select one item from a large list of records. For instance, on an order entry form, the user must be able to select the customer from a large list of customers. For performance reasons, it would be insane to just load a DropDownList with 50000 items. The other alternative would be to open another form where the user would be able to search and select the desired customer. I've used this approach before, but the users usually found it very annoying and were constantly demanding a better solution.

Solution

The AJAX boom made it an easy problem to solve. An AutoSuggest textbox, where the user would type a part of the desired customer name and the control would show the matches, would fit perfectly here. Yet, I couldn't find one that would completely fill my needs. I needed an AutoSuggest textbox with the following features:

  • Built-in validation
  • Template-based content
  • Ability to bind to all sorts of objects (collection, datatables, etc)
  • Ability to work like a DropDownList
  • Smooth integration with Anthem.NET
  • Ability to show a header
  • No need to call webservices

The Control

This custom control is based on the Anthem library. I decided to use Anthem because at that time I was already using it on my projects. The Atlas Project was not mature enough and Anthem seemed much easier and more powerful. The Anthem library now has an official AutoSuggest control, but I haven't checked it out yet. If you are not familiar with Anthem.NET yet, I encourage you to check it out. It's pretty simple and works great. The JavaScript OO model was based on another free AutoSuggest control that I found on this web site. I found it pretty nice, but it was missing some functionality I needed.

Setting Up

To use this AutoSuggest control, you will need to reference both the Anthem.dll and Anthem.AutoSuggest.dll in your project. The download contains these DLLs, the AutoSuggest source code and an example project source code.

Using the Code

The first thing you need to do is to add the control to your page. This can be done through drag and drop or by writing the tags directly into the ASPX source file. Since I didn't add any relevant design-time support, I think you'd better stick with the ASPX source code. In this example we are using the AutoSuggest control to display the names of various bands and the user must select his favorite one.

ASP.NET
<Anthem:AutoSuggestBox runat="server" 
    ID="asbFavoriteBand" DataKeyField="ID" 
    TextBoxDisplayField="Name" AutoCallBack="true" 
    ItemNotFoundMessage="Item not found!" >
    <itemtemplate>
        <%# ((Band)Container.DataItem).Name %>
    </ItemTemplate>
</Anthem:AutoSuggestBox>

I think most of the property names are self-explanatory. I encourage you to go through the properties and play with them. For those unfamiliar with the Anthem.NET library, the AutoCallBack attribute tells that after the selected value has changed, the control will trigger a callback to the server. It's equivalent to the AutoPostBack property of the regular ASP.NET controls. Notice that you can use the ItemTemplate in the same manner in which you use it with the Repeater, DataList or DataGrid controls. The DataKeyField property tells the control which field it will use to set the SelectedValue property.

After adding the control to the page, you should set up the event handlers. The most important event you should handle is the TextChanged event. This is where you are going to fill the suggested list. Another important event is the SelectedValueChanged. This event is fired whenever you change the current value. To wire up these handlers, you can write the following code in the OnInit method:

C#
protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    this.asbFavoriteBand.TextChanged += 
        new Anthem.AutoSuggestBox.TextChangedEventHandler(
        asbFavoriteBand_TextChanged);

    this.asbFavoriteBand.SelectedValueChanged += 
        new Anthem.AutoSuggestBox.SelectedValueChangedHandler(
        asbFavoriteBand_SelectedValueChanged);
}

Here's the code to handle the TextChanged event:

C#
void asbFavoriteBand_TextChanged(object source, 
        Anthem.AutoSuggestEventArgs e)
{
    //Creates a dataview from a datatable
    DataView dv = new DataView(_dtBands);

    //Filters the datatable based on the CurrentText property
    dv.RowFilter = string.Format("BandName LIKE '%{0}%'", e.CurrentText);

    //Sets the dataview as the control's datasource
    asbFavoriteBand.DataSource = dv;
    asbFavoriteBand.DataBind();
}

In the snippet above, you can use any data source on the AutoSuggest control. Usually you would query the database for the result set. It's when you call the DataBind method that the suggested list appears on the screen.

Points of Interest

There are, definitely, some nice aspects of this control that demand a closer look at how .NET controls work, like using embedded web resources (images, JavaScript and CSS files), supporting template based content, triggering events and handling the JavaScript integration.

Object-Oriented JavaScript is also worth a look. It really makes things easier.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior) Intelligent Coder
Canada Canada
I've been developing .NET enterprise applications since 2000.

I am originally from Rio de Janeiro and I am currently working at http://www.intelligentcoder.com in Ontario.

I also have my own startup where we offer client intake forms.

Comments and Discussions

 
GeneralRe: IE 7 problem .. Pin
kansas8116-Jul-07 10:21
kansas8116-Jul-07 10:21 
QuestionWhat can I do to improve speed? Pin
jonefer5-Jul-07 10:49
jonefer5-Jul-07 10:49 
AnswerRe: What can I do to improve speed? Pin
Cassio Mosqueira5-Jul-07 11:51
Cassio Mosqueira5-Jul-07 11:51 
GeneralRe: What can I do to improve speed? Pin
jonefer5-Jul-07 13:53
jonefer5-Jul-07 13:53 
GeneralRe: What can I do to improve speed? Pin
Cassio Mosqueira5-Jul-07 16:25
Cassio Mosqueira5-Jul-07 16:25 
GeneralRe: What can I do to improve speed? Pin
jonefer5-Jul-07 21:46
jonefer5-Jul-07 21:46 
GeneralRe: What can I do to improve speed? Pin
Cassio Mosqueira6-Jul-07 9:10
Cassio Mosqueira6-Jul-07 9:10 
GeneralRe: What can I do to improve speed? Pin
jonefer6-Jul-07 10:19
jonefer6-Jul-07 10:19 
Ok, we're getting closer - the speed is now acceptable - but now here's the issue:

I'm reading CostCenters which look like this:
so if I start typing 01, I get this...

010071001 Hosp Flr 1 West, H1W
010071002 Hosp Flr 1 East- H1E
010071003 Hosp Flr 2 East, H2E
010071004 Hosp Flr 2 West, H2W
010071005 Hosp Flr 4 East, H4E...


However, since we have only selected 20, (BEFORE) I start typing..
it doesn't find these:
080033851 Ground Department
080033852 Facility Maintenance

Here is the portion of Code that I have adjusted to fit my needs:


Private Sub asbCC_TextChanged(ByVal source As Object, ByVal e As Anthem.AutoSuggestEventArgs)
'Creates a dataview of the session's datatable

Dim purchaseLogic As New PurchasingBLL
Dim dt As DataTable = purchaseLogic.GetCC


Dim dv As New DataView(dt)


'Filters the datatable based on the CurrentText property
dv.RowFilter = String.Format("FullCCNum LIKE '{0}%'", e.CurrentText)
'Sets the dataview as the control's datasource
asbCC.DataSource = dv
asbCC.DataBind()

End Sub


This is what the query looks like:
SELECT TOP 20 FullCCNum, CC_Descr
FROM dbo.vCC



GeneralRe: What can I do to improve speed? Pin
Cassio Mosqueira6-Jul-07 11:03
Cassio Mosqueira6-Jul-07 11:03 
GeneralRe: What can I do to improve speed? Pin
jonefer6-Jul-07 11:26
jonefer6-Jul-07 11:26 
GeneralRe: What can I do to improve speed? Pin
Cassio Mosqueira6-Jul-07 11:45
Cassio Mosqueira6-Jul-07 11:45 
GeneralRe: What can I do to improve speed? Pin
jonefer6-Jul-07 11:52
jonefer6-Jul-07 11:52 
QuestionRe: What can I do to improve speed? Pin
jonefer12-Jul-07 9:49
jonefer12-Jul-07 9:49 
AnswerRe: What can I do to improve speed? Pin
tmrhymer11-Jul-07 8:50
tmrhymer11-Jul-07 8:50 
QuestionRe: What can I do to improve speed? Pin
jonefer13-Jul-07 15:11
jonefer13-Jul-07 15:11 
AnswerRe: What can I do to improve speed? Pin
tmrhymer15-Jul-07 14:27
tmrhymer15-Jul-07 14:27 
AnswerRe: What can I do to improve speed? Pin
jonefer26-Jul-07 10:46
jonefer26-Jul-07 10:46 
GeneralRe: What can I do to improve speed? [modified] Pin
tmrhymer27-Jul-07 9:51
tmrhymer27-Jul-07 9:51 
QuestionWhere's the sample in the picture? Pin
jonefer29-Jun-07 15:36
jonefer29-Jun-07 15:36 
AnswerRe: Where's the sample in the picture? Pin
Cassio Mosqueira30-Jun-07 6:29
Cassio Mosqueira30-Jun-07 6:29 
QuestionScrolling to suggestions Pin
Priyatam K24-Jun-07 23:24
Priyatam K24-Jun-07 23:24 
QuestionRe: Scrolling to suggestions Pin
Priyatam K5-Jul-07 19:13
Priyatam K5-Jul-07 19:13 
AnswerRe: Scrolling to suggestions Pin
Cassio Mosqueira6-Jul-07 8:59
Cassio Mosqueira6-Jul-07 8:59 
GeneralRe: Scrolling to suggestions Pin
Priyatam K8-Jul-07 15:25
Priyatam K8-Jul-07 15:25 
GeneralRe: Scrolling to suggestions Pin
gri413-Aug-07 23:59
gri413-Aug-07 23:59 

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.