Click here to Skip to main content
Email Password   helpLost your password?

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:

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

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!

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralDoes not work with RequiredFieldValidator
fronjm05
11:02 4 Mar '10  
Have you tried to use this on a textbox that has an ASP.NET RequiredFieldValidator attached to it? Doesnt seem to work. Darn. Any thoughts?
GeneralHow can I achieve this in .net2.0?
Mohammmed Farooq
2:45 27 Jul '09  
Thanks for the nice code!

Can you please reply my concerns
http://forums.asp.net/t/1451626.aspx

Thanks,

Mohd Farooq

GeneralHow can i use your solution with DynamicPopulate
Member 4624136
21:35 21 Jul '09  
My page has Listview that 's contain a lot of data (about 300000 up)

How can i implement with DynamicPopulateExtender to set Thread.Sleep and show loading icon?

sorry,i'm poor in english


thk for advance.
GeneralRe: How can i use your solution with DynamicPopulate
Remy Blaettler
23:27 21 Jul '09  
I'm actually not sure about your question. This is mainly about showing results while you type your search query.

Remy Blaettler
Chief of the System
Supertext AG

GeneralGreat job!
xrobx
0:01 3 Jun '08  
Thank you alot. This is really great! Big Grin
GeneralRe: Great job!
Remy Blaettler
4:53 26 Jul '08  
You are welcome. Was my pleasure Smile

Remy Blaettler
Chief of the System
Supertext AG

Answer"DefaultButton" property is not working [modified]
Member 3827402
5:43 30 Apr '08  
Hi,

This is a great job first of all.
I had a problem with submit button. "DefaultButton" property of the Panel becomes disable.

I solved this problem by manually adding this:

TextBox1.Attributes.Add(
"onkeydown",
"if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('"+ FindImageButton.UniqueID +"').click(); return false;}} else {return true;}"); Smile

Burki

modified on Thursday, May 1, 2008 7:58 AM

QuestionAutocompleteExtender
Andrei Rinea
0:33 19 Apr '08  
Is there something different from the Autocomplete Extender in the ASP.NET AJAX Toolkit?


GeneralRe: AutocompleteExtender
Remy Blaettler
8:37 21 Apr '08  
Yes, it is different. The idea and the code is very similar, but the AutocompleteExtender gives you a list that is attached to the TextBox, while my control normally updates a ListView or a GridView below the TextBox on the page.

Compare the the demos:

http://www.asp.net/ajax/ajaxcontroltoolkit/samples/AutoComplete/AutoComplete.aspx[^]

http://remy.supertext.ch/RemyExamples/DelayedSubmitExample/Default.aspx[^]

Remy Blaettler
Chief of the System
Supertext AG

AnswerRe: AutocompleteExtender
Andrei Rinea
9:40 21 Apr '08  
Thanks for clarifying Smile


GeneralRe: AutocompleteExtender
Remy Blaettler
17:49 24 Apr '08  
You are welcome Smile

Remy Blaettler
Chief of the System
Supertext AG

GeneralI think there is a mistake.
Matrix
7:29 15 Apr '08  
I can't find where you set the this._text value,so I guess if the method "_onkeydown" should be modified:

_onkeydown: function(ev) {
this._text = this.get_element().value.
}


That's right?? Smile
GeneralRe: I think there is a mistake.
Remy Blaettler
3:40 17 Apr '08  
It is set in the onTimerTick function.

_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();
}
},

It's mainly to prevent a submit if a non Character key was pressed or nothing actually changed.
Like you deleted the last character and then added the same one again.
If you add it to the onkeydown function, it will not be able to detect the change from the last
submission.

Remy Blaettler
Chief of the System
Supertext AG


Last Updated 11 Apr 2008 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010