Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Using the AutoComplete Extender to Automatically Perform a PostBack

4.29/5 (6 votes)
7 Apr 2010CPOL2 min read 5  
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)