Click here to Skip to main content
Click here to Skip to main content

AJAX AutoComplete/AutoSuggest TextBox

By , 2 Oct 2007
 
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.

<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:

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:

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

About the Author

Cassio Mosqueira
Software Developer (Senior)
Canada Canada
Member
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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionValue not getting in the normal asp:textbox [modified] PinmemberChandrashekar Srinivasan16 Apr '12 - 18:39 
GeneralOnclick in the elements Pinmembersmntpzl3 Mar '11 - 0:10 
GeneralAppreciate your work PinmemberXITIJ_CODE_0417 Feb '11 - 9:12 
QuestionIs it possible to set AutoSuggestBox ReadOnly? PinmemberMember 404403014 Nov '10 - 16:29 
Generalvisible property of anthem set runtime and its event are not working with update panel (3.5) [modified] Pinmembergunasathia22 Sep '10 - 19:00 
Questionhow work in asp.net with vb.net Pinmemberste199017 May '10 - 23:32 
GeneralSpeed problem Pinmemberterrix7712 May '10 - 20:59 
Questioncan you helme version php Pinmemberariodoni22 Mar '10 - 10:59 
GeneralScroll Bug PinmemberKeshav Fulpagare14 Sep '09 - 23:02 
QuestionWill it work if I add it dynamically? PinmemberMember 42823403 Sep '09 - 7:00 
GeneralInvitation for new .NET Programming resource website a Author and supporter PinmemberRavenet1 Aug '09 - 18:56 
GeneralThank you! [modified] PinmemberEmilotti17 Jun '09 - 15:14 
Questionscrolling problem not solved yet ? Pinmemberrembrant8521 May '09 - 3:31 
Questionis this source code require any licence agreement? PinmemberSandeep Ramani30 Mar '09 - 20:55 
QuestionProblems of using Anthem.Net with pages having master page PinmemberManish Partey24 Mar '09 - 20:29 
QuestionHow to enable/disable an anthem:autosuggestbox? Pinmemberarchy2k813 Mar '09 - 23:32 
QuestionDesigner support & matching items in the data source Pinmemberhumba13 Mar '09 - 7:53 
GeneralSelected value disappears on TAB (On Key UP) Pinmember2irfanshaikh25 Feb '09 - 3:43 
Questionhow can we implement a choice in the code Pinmemberjitendra v patil16 Jan '09 - 19:44 
QuestionJavascript Error with UpdatePanel PinmemberJ Stump7 Jan '09 - 14:54 
Questionlayout Problem with IE6 Pinmemberbhavik.vaishnani6 Jan '09 - 0:41 
GeneralEvent SelectedValueChanged does only raise when clicking with mouse Pinmemberd00_ape8 Nov '08 - 2:35 
GeneralOnly fill value when click on item! Pinmemberhapytran5 Nov '08 - 22:38 
GeneralAny suggestion to how to hide the suggestions box when the text has changed Pinmemberthomasa5 Nov '08 - 5:10 
GeneralVS 2003 doesn't allow me to add these dll's :( PinmemberVenkata Siva20 Oct '08 - 14:13 
GeneralNice! PinmemberJasonC7213 Oct '08 - 5:18 
Questionhow can I use a mysql database in this sample? Pinmemberjason_wang113 Oct '08 - 21:19 
QuestionHow can I find get a control inside HeaderTemplate? Pinmembermoufis1 Jul '08 - 11:08 
GeneralCouldnt load control Pinmemberairbase24 Jun '08 - 3:48 
QuestionLinking within the autocomplete Pinmemberddoug2323 Jun '08 - 8:12 
QuestionI find a small bug [modified] Pinmemberlaoyingisme11 Jun '08 - 17:36 
GeneralSetting another control based on this control problem PinmemberStonkers4 Jun '08 - 12:22 
QuestionUsing Database table instead of Static datatable PinmemberGiveDcode9 Apr '08 - 1:48 
QuestionBug Autosuggest project Pinmembersameer_wanwey25 Mar '08 - 20:26 
GeneralCalling a client side function upon SelectedValueChange PinmemberMember 189180224 Mar '08 - 11:44 
GeneralAutosuggest selecting with arrow and enter key fix Pinmembertmrhymer12 Mar '08 - 7:26 
GeneralProblem with space PinmemberPrithwish Biswas6 Mar '08 - 4:50 
Generaltwo autosuggest in one page Pinmemberpn_renju22 Feb '08 - 1:21 
Question"'JSAutoSuggestBox' is undefined". Java script error Pinmembersaro_ja19 Feb '08 - 7:54 
GeneralOnFocus of Autosuggest Pinmembertmrhymer3 Jan '08 - 8:32 
QuestionAutosuggest Problem Pinmembersameer_wanwey10 Dec '07 - 19:03 
GeneralAutosuggest results causing links to not work Pinmembertmrhymer7 Dec '07 - 6:21 
QuestionClear Text PinmemberRon Lock7 Dec '07 - 5:33 
QuestionPre/Post callback Functions Not Firing - Show Spinny Pinmembercbone21 Nov '07 - 8:39 
Questionhow can i disable textboxes of datalist item templete by javascript PinmemberHarun.Net14 Nov '07 - 19:37 
GeneralImplementing Scrolling - ON AutoSuggest Pinmemberimaginedesgin24 Oct '07 - 9:30 
GeneralNew ie 6 issue Pinmembertmrhymer19 Oct '07 - 12:21 
GeneralPerfect Pinmemberftoomi8 Oct '07 - 15:49 
QuestionError evaluating client-side script! PinmemberSamnang Chhun4 Oct '07 - 23:44 
GeneralPutting Javascript into JS file Pinmemberbgates19703 Oct '07 - 8:51 

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 2 Oct 2007
Article Copyright 2007 by Cassio Mosqueira
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid