Click here to Skip to main content
15,879,095 members
Articles / Web Development / ASP.NET

Using the AutoComplete Extender to Automatically Perform a PostBack

Rate me:
Please Sign up or sign in to vote.
4.29/5 (6 votes)
7 Apr 2010CPOL2 min read 97.1K   16   7
Demonstrates how to use the AutoComplete Extender to automatically perform a PostBack

Introduction

The ASP.NET Ajax Control Toolkit provides a very helpful AutoCompleteExtender control that allows developers to easily provide Google Suggest like auto-complete features, and even provides a key / value mode for drop-down like behavior. (You can read a fantastic overview of this functionality here.)

One drawback of this control, however, is that it does not provide native functionality to automatically initiate a server side post-back inside an update panel when the user selects an item in the suggestion list. This article will briefly review how to implement this functionality.

Background

In order to avoid repetition, this code builds on the examples built in the "Implementing Google like Suggestion using Autocomplete Extender" article.

Using the Code

If you ever want a user's selection on an AutoComplete control to trigger a post back to your web server, you will immediately notice that the control does not contain a built in server event for this action. Fortunately for us, however, the control does have a client side event. We can use this client side event to send the selected value to the server through a hidden control using JavaScript.

This example uses an UpdatePanel because most applications that use an Ajax based control to select a value will also expect that the page will not refresh once a selection is made. The UpdatePanel, however, is not needed. The web controls in our example can be laid out very simply:

ASP.NET
<asp:updatepanel id="update1" runat="server">
    <contenttemplate>
    
    <asp:hiddenfield id="hdnValue" onvaluechanged="hdnValue_ValueChanged" runat="server"/>
    
    <asp:textbox runat="server" id="myTextBox" width="300" autocomplete="off" />
    <ajaxtoolkit:AutoCompleteExtender runat="server" 
        behaviorid="AutoCompleteEx" 
        id="autoComplete1" 
        targetcontrolid="myTextBox" 
        servicepath="AutoComplete.asmx" 
        servicemethod="GetCompletionList" 
        scriptpath="AutoCompleteBehavior.js" 
        minimumprefixlength="2" completioninterval="1000" 
        enablecaching="true" completionsetcount="20" 
        delimitercharacters=";, :" 
        showonlycurrentwordincompletionlistitem="true" 
        onclientitemselected="OnContactSelected">
    </ajaxtoolkit:autocompleteextender>
            
    
   </contenttemplate>     
</asp:updatepanel> 

Of particular importance is the OnClientItemSelected attribute to the autocomplete control. This property indicates that the OnContactSelected JavaScript function will be called when the user makes a selection. The implementation of our method differs from that of the original article because it sets our hidden field value and causes a postback of the form.

JavaScript
    function OnContactSelected(source, eventArgs) {
        
    var hdnValueID = "<%= hdnValue.ClientID %>";

    document.getElementById(hdnValueID).value = eventArgs.get_value();
    __doPostBack(hdnValueID, "");
} 

The above JavaScript function sets the value of our hidden field, and uses that same hidden field as the impetus of the ASP.NET framework's <code>__doPostBack call. Because the hidden field has a handler for the OnValueChanged event, the code will call the following event handler on the server side. We can use this event to look up further information from some persistent store, update other fields on the page, or any other needed functionality.

C#
protected void hdnValue_ValueChanged(object sender, EventArgs e)
{
    string selectedWidgetID = ((HiddenField)sender).Value;

    Widget w = MyEntityService.GetWidget(selectedWidgetID);

    ///populate the form based on retrieved data
} 

History

  • 7th April, 2010: Initial post

License

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


Written By
Technical Lead CTS, Inc. Birmingham, AL
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Shibasis Sengupta24-Sep-13 3:30
professionalShibasis Sengupta24-Sep-13 3:30 
great help... thanks a lot
QuestionThanks man it helped me Pin
SelvinTurai4-Apr-13 15:05
SelvinTurai4-Apr-13 15:05 
GeneralMy vote of 4 Pin
Pham Dinh Truong28-Mar-13 1:12
professionalPham Dinh Truong28-Mar-13 1:12 
QuestionThanks very much Pin
Mythos725-Oct-12 4:34
Mythos725-Oct-12 4:34 
QuestionIs there a way to call a generic method on the server? Pin
George Ceaser19-Jan-11 9:29
George Ceaser19-Jan-11 9:29 
Generalgetting id field Pin
Member 33200607-Jul-10 3:00
Member 33200607-Jul-10 3:00 
GeneralRe: getting id field Pin
njoshi19877-Nov-11 21:58
njoshi19877-Nov-11 21:58 

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.