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

Using the AutoComplete Extender to Automatically Perform a PostBack

, 7 Apr 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
Demonstrates how to use the AutoComplete Extender to automatically perform a PostBack


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.


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:updatepanel id="update1" runat="server">
    <asp:hiddenfield id="hdnValue" onvaluechanged="hdnValue_ValueChanged" runat="server"/>
    <asp:textbox runat="server" id="myTextBox" width="300" autocomplete="off" />
    <ajaxtoolkit:AutoCompleteExtender runat="server" 
        minimumprefixlength="2" completioninterval="1000" 
        enablecaching="true" completionsetcount="20" 
        delimitercharacters=";, :" 

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.

    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 __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.

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


  • 7th April, 2010: Initial post


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


About the Author

Jonathan Cortis
Technical Lead CTS, Inc. Birmingham, AL
United States United States
No Biography provided

Comments and Discussions

GeneralMy vote of 5 PinmemberShibasis Sengupta24-Sep-13 4:30 
QuestionThanks man it helped me PinmemberSelvinTurai4-Apr-13 16:05 
GeneralMy vote of 4 PinmemberMr. Truong Pham28-Mar-13 2:12 
QuestionThanks very much PinmemberMythos725-Oct-12 5:34 
QuestionIs there a way to call a generic method on the server? PinmemberMember 137306319-Jan-11 10:29 
Generalgetting id field PinmemberMember 33200607-Jul-10 4:00 
Article looks very good. i have similar requirement if you can help.., I am showing employee name in autocomplete text box and i need employee id to be stored in another hidden field. i tried the method you have explained but its not working. in web service i am passing only two parameters i.e.
string prefixText, int count
do i need to make any changes as i cant see your web service code.
Thanks in advance
Priya :(
GeneralRe: getting id field Pinmembernjoshi19877-Nov-11 22:58 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150327.1 | Last Updated 7 Apr 2010
Article Copyright 2010 by Jonathan Cortis
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid