5,276,156 members and growing! (18,185 online)
Email Password   helpLost your password?
Web Development » Ajax and Atlas » Controls     Intermediate License: The Code Project Open License (CPOL)

See search results as you type - An ASP.NET AJAX control

By Remy Blaettler

A search box that updates the page with search results as you type.
C# (C# 2.0, C# 3.0, C#), Javascript, CSS, HTML, .NET (.NET, .NET 2.0, .NET 3.5, .NET 3.0), ASP, ASP.NET, Ajax, Design, Dev

Posted: 11 Apr 2008
Updated: 11 Apr 2008
Views: 5,297
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
5 votes for this Article.
Popularity: 3.03 Rating: 4.33 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
1 vote, 20.0%
3
1 vote, 20.0%
4
3 votes, 60.0%
5

DelayedSubmitDemo

Introduction

Highrise and other new AJAX enabled tools have this feature that when you type in a search query, it automatically updates the search results below. It's almost like an auto-complete box, but with the full results on the page instead of a dropdown list below the control. Unfortunately, I didn't find anything like this in the Microsoft ASP.NET AJAX Toolkit. So, I decided that this was a good way to finally dive into the AJAX Control Toolkit.

Here is the result: Live Demo.

It's my first AJAX control, so I'm sure there is room for improvements. I've tested it in IE7, FF, and Safari. Seems to work fine in all of them.

Background

I used the Membership Editor Example from Peter Keller and TextChangedBehavior.js from Garbin as my inspiration and resource, but started with a VS ASP.NET AJAX Control Project to get the framework up and running.

The Use Case

The user starts typing his search query into the textbox. After he types in the first few characters of the keyword he is looking for, he pauses. Magically, all search results that fit his query so far appear below. If he types in more of the keyword, the results are updated accordingly.

Example: A user is seaching for a contact. He types Bl and stops. The names that now show up could include:

  • Blaettler
  • Blatt
  • Blackmore
  • Bloomfield
  • Blodgett
  • Blum

Now, he types another letter, let's say, o, and the list reduces itself to:

  • Bloomfield
  • Blodgett

The Control

There is really only JavaScript code that has functionality. The only thing the code does is start a time on the keyup event, stop the time on keydown, and after the timer fires, it executes the onchange method of the associated TextBox. This way, we get a delayed postback after the user stops typing and not lots of postbacks when he's still writing something.

_onkeyup : function(ev) {
    var k = ev.keyCode ? ev.keyCode :
                ev.rawEvent.keyCode;
    if (k != Sys.UI.Key.Tab) {
        this._timer.set_enabled(true);
    }
},

_onkeydown : function(ev) {
    this._timer.set_enabled(false);
},

_onTimerTick : function(sender, eventArgs) {
    this._timer.set_enabled(false);

    if(this._text != this.get_element().value) {
        this._text = this.get_element().value;

        this.get_element().onchange();
    }
},

Everything else is just setup and teardown code. There is one property in the C# file called Timeout. This determines how long of a pause the user needs to make until the search results get updated.

Using the Code

Just add this control like any other Extender to your ASPX page, set the TargetControlID, and add a TextChanged handler to the search textbox.

<cc1:DelayedSubmitExtender 
    ID="DisableButtonExtender1" 
    runat="server" 
    Timeout="1000" 
    TargetControlID="TextBox1"/>
<asp:TextBox 
    ID="TextBox1" 
    runat="server" 
    AutoPostBack="True" 
    OnTextChanged="TextBox1_TextChanged" 
    Columns="50"></asp:TextBox>        
<br />
List of matching words:<br />

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="TextBox1" />
    </Triggers>
    <ContentTemplate>
        <asp:Repeater ID="Repeater1" runat="server">
            <HeaderTemplate><ul></HeaderTemplate>
            <ItemTemplate>
                <li><%#DataBinder.Eval(Container, "DataItem[0]")%></li>
            </ItemTemplate>
            <FooterTemplate></ul></FooterTemplate>
        </asp:Repeater>
    </ContentTemplate>
</asp:UpdatePanel>

I would then wrap the search results area in an UpdatePanel. If the TextBox is outside, just add an AsyncPostBackTrigger to the UpdatePanel. Inside the UpdatePanel, you can present your search results. For my example, I just have a list of words.

Code-behind

protected void TextBox1_TextChanged(object sender, EventArgs e)
{
    List<string[] /> items = new List<string[] />();

    for (int i = 0; i < wordlist.Length; i++ )
    {
        if (wordlist[i].StartsWith(
                TextBox1.Text, 
                StringComparison.OrdinalIgnoreCase))
        {
            string[] item = new string[1];
            item[0] = wordlist[i];

            items.Add(item);
        }
    }

    Repeater1.DataSource = items;
    Repeater1.DataBind();
}

In the TextChanged handler on the server side, you need to add your specific search code. As mentioned above, I just display a list of words with a Repeater control. The list is contained in a string array called wordlist.

I would recommend to just have a look at the example. It's very simple, and should show you how this works.

Please let me know how it works and if it is of any use. Good luck!

License

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

About the Author

Remy Blaettler


Remy Blaettler (or Rémy Blättler in Swiss German writing) works as the Chief of the System for Supertext AG.

Supertext is the first online copywriting agency and has over 300 professional freelance writers, journalists, proofreaders and translaters in ints network.


Remy started dabbling with Computers when he was 12 on his fathers 286 PC with Basic, he soon moved to Turbo Pascal and then to C++. The last 3 years were dedicated to C# and some ASP.NET.
Occupation: Web Developer
Location: United States United States

Other popular Ajax and Atlas articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 8 of 8 (Total in Forum: 8) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralGreat job!memberxrobx0:01 3 Jun '08  
Answer"DefaultButton" property is not working [modified]memberMember 38274025:43 30 Apr '08  
QuestionAutocompleteExtendermemberAndrei Rinea0:33 19 Apr '08  
GeneralRe: AutocompleteExtendermemberRemy Blaettler8:37 21 Apr '08  
AnswerRe: AutocompleteExtendermemberAndrei Rinea9:40 21 Apr '08  
GeneralRe: AutocompleteExtendermemberRemy Blaettler17:49 24 Apr '08  
GeneralI think there is a mistake.memberMatrix7:29 15 Apr '08  
GeneralRe: I think there is a mistake.memberRemy Blaettler3:40 17 Apr '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 11 Apr 2008
Editor: Smitha Vijayan
Copyright 2008 by Remy Blaettler
Everything else Copyright © CodeProject, 1999-2008
Web19 | Advertise on the Code Project