Click here to Skip to main content
15,867,568 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 876.8K   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

 
QuestionClear Text Pin
Ron Lock7-Dec-07 5:33
Ron Lock7-Dec-07 5:33 
QuestionPre/Post callback Functions Not Firing - Show Spinny Pin
cbone21-Nov-07 8:39
cbone21-Nov-07 8:39 
Questionhow can i disable textboxes of datalist item templete by javascript Pin
Harun.Net14-Nov-07 19:37
Harun.Net14-Nov-07 19:37 
GeneralImplementing Scrolling - ON AutoSuggest Pin
imaginedesgin24-Oct-07 9:30
imaginedesgin24-Oct-07 9:30 
GeneralRe: Implementing Scrolling - ON AutoSuggest Pin
Cassio Mosqueira24-Oct-07 15:48
Cassio Mosqueira24-Oct-07 15:48 
GeneralNew ie 6 issue Pin
tmrhymer19-Oct-07 12:21
tmrhymer19-Oct-07 12:21 
GeneralRe: New ie 6 issue Pin
imaginedesgin24-Oct-07 9:41
imaginedesgin24-Oct-07 9:41 
GeneralRe: New ie 6 issue Pin
tmrhymer29-Oct-07 3:52
tmrhymer29-Oct-07 3:52 
I'm not sure i get what you mean. The way i am using this Autosuggest control is on the right hand side of a page in a sidebar. It rests inside of a div container, has position:absolute set on it and a z index value of 998. right below the autosuggest control is another anthem based control, the anthem panel. In the anthem panel is where users will see pre-data based on the selection they make in the autosuggest control. Say a user starts to type in my name, sees it, clicks it. In the anthem panel the user will see some information about me and a link to an even more detailed page. In ie6 the autosuggest controls results are what covers up my detail link, even after the suggest box is not being displayed, which leads me to believe that it is left in its last state before it is hidden. What would be really helpful is if the suggest box had a z-index property. When the suggest box results are visible they would be set to like 1000, and when the suggest box results are not visible it would be set to 900. So there could be a way to put things below it in ie6 without conflict.
GeneralPerfect Pin
ftoomi8-Oct-07 15:49
ftoomi8-Oct-07 15:49 
QuestionError evaluating client-side script! Pin
Samnang Chhun4-Oct-07 23:44
Samnang Chhun4-Oct-07 23:44 
GeneralPutting Javascript into JS file Pin
bgates19703-Oct-07 8:51
bgates19703-Oct-07 8:51 
GeneralRe: Putting Javascript into JS file Pin
Cassio Mosqueira3-Oct-07 14:50
Cassio Mosqueira3-Oct-07 14:50 
GeneralRe: Putting Javascript into JS file Pin
bgates19705-Oct-07 7:22
bgates19705-Oct-07 7:22 
QuestionIssue with Master page? Pin
Samnang Chhun30-Sep-07 22:11
Samnang Chhun30-Sep-07 22:11 
AnswerRe: Issue with Master page? Pin
Cassio Mosqueira1-Oct-07 16:39
Cassio Mosqueira1-Oct-07 16:39 
QuestionCan't bind datasource to Repeater that has AutoComplete control outside Page_Load? Pin
Samnang Chhun30-Sep-07 15:59
Samnang Chhun30-Sep-07 15:59 
QuestionCan't enable or disable TextChanged event from AutoComplete? [modified] Pin
Samnang Chhun27-Sep-07 20:51
Samnang Chhun27-Sep-07 20:51 
AnswerRe: Can't enable or disable TextChanged event from AutoComplete? Pin
Samnang Chhun30-Sep-07 15:50
Samnang Chhun30-Sep-07 15:50 
QuestionCan't use AJax AutoComplete in DataList? Pin
Samnang Chhun26-Sep-07 22:22
Samnang Chhun26-Sep-07 22:22 
AnswerRe: Can't use AJax AutoComplete in DataList? Pin
Cassio Mosqueira27-Sep-07 4:28
Cassio Mosqueira27-Sep-07 4:28 
GeneralRe: Can't use AJax AutoComplete in DataList? Pin
Samnang Chhun27-Sep-07 15:52
Samnang Chhun27-Sep-07 15:52 
GeneralRe: Can't use AJax AutoComplete in DataList? Pin
Cassio Mosqueira27-Sep-07 18:43
Cassio Mosqueira27-Sep-07 18:43 
QuestionRe: Can't use AJax AutoComplete in DataList? Pin
Nihar Ranjan Jena12-Nov-07 19:37
Nihar Ranjan Jena12-Nov-07 19:37 
QuestionCan't get SelectedValue when user type correctly? Pin
Samnang Chhun26-Sep-07 18:31
Samnang Chhun26-Sep-07 18:31 
AnswerRe: Can't get SelectedValue when user type correctly? Pin
Cassio Mosqueira26-Sep-07 18:40
Cassio Mosqueira26-Sep-07 18:40 

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.