Welcome to MSDN Blogs Sign in | Join | Help

Cascading AutoComplete

We have received a lot of feedback from users about not being able to pass additional information to the AutoComplete webservice which limits their usage of the extender. We decided to absorb that input and add support to all extenders that issue XmlHttpRequests the option of passing in data above and beyond what is allowed by the standard webservice parameters, something that closely resembles the DynamicPopulate style support, to provide to flexible context. With this support in, you will be able to bind the AutoComplete extender to any controls on the page and have a richer, contextual and relevant set of results returned by the AutoComplete webservice. It will be available in the next release of the Toolkit around the end of May.

I played around with this new feature to see if something interesting could be done with AutoComplete. There was a question about getting AutoComplete to work like CascadingDropDown on the forums which I had earlier considered but dismissed given that it was not easily possible; turns out that the scenario now lights up naturally. I have written a basic version of CascadingAutoComplete and am attaching the project. I have not used ASP.NET DataBinding but simple javascript to set the values of the ContextKey so this solution should work without ASP.NET with little changes. It is not possible to have a fully functional CascadingDropDown with AutoComplete since it is a TextBox and not a DropDown but the input checks can provide a similar experience. You could always add validators or other Toolkit extenders like MaskedEdit, ValidatorCallout out FilteredTextBox to prevent invalid input. There are multiple ways of approaching this and you will need to find the solution that best serves your website's needs. The Toolkit now enables you do more with the extenders. The other controls that can take in additional data are SlideShow and CascadingDropDown itself.

The sample demonstrates a simple lookup scenario where the allowed values in the second textbox are tied to the value in the first one. The second textbox uses the text in the first one to perform a webservice call and retrieve results. Check it out. If you are a movie freak or if you like to read Hollywood gossip, there just might be something more than AJAX to look forward to in the sample. Let us know what you think. We are listening.

Published Wednesday, October 10, 2007 12:31 AM by kirtid
Attachment(s): CascadingAutoComplete.zip

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

Tuesday, October 09, 2007 7:41 PM by Techy News Blog » Cascading AutoComplete

# Techy News Blog » Cascading AutoComplete

Tuesday, October 09, 2007 8:10 PM by MSDN Blog Postings » Cascading AutoComplete

# MSDN Blog Postings » Cascading AutoComplete

Tuesday, October 09, 2007 8:10 PM by MSDN Blog Postings » Cascading AutoComplete

# MSDN Blog Postings » Cascading AutoComplete

Thursday, December 27, 2007 2:59 PM by Oran

# re: Cascading AutoComplete

Thanks for the helpful posting!

I copied your javascript and I got it to work on my text boxes but it isn't working the way I thought it would.

When the user changes the value of the textbox that holds the context key and moves to the next textbox the autocomplete sometimes will use an "old" list of values from the last context key. I tried setting a break point on the web service that retieves the list from the database and in these instances it doesn't get fired. I assume the list is cached. I didn't experience this in your test website. Do you know what the problem could be?

Here is my code:

Webservice:

<WebMethod()> _

  Public Function GetPolicyNumbers(ByVal prefixText As String, ByVal count As Integer, ByVal contextKey As String) As String()

     Dim strsql As String = "Select DISTINCT TOP 15 PolicyNumber from tbl_Policy Where PolicyNumber like @prefixText AND PolicySymbol = @contextKey"

Dim daSQL As New SqlDataAdapter(strsql, My.Settings.ConnectionString)

     daSQL.SelectCommand.Parameters.Add("@prefixText", SqlDbType.VarChar, 50).Value = "%" + prefixText + "%"

     daSQL.SelectCommand.Parameters.Add("@contextKey", SqlDbType.VarChar, 50).Value = contextKey

     Dim dt As New DataTable

     daSQL.Fill(dt)

     Dim items(dt.Rows.Count - 1) As String

     Dim i As Integer = 0

     Dim dr As DataRow

     For Each dr In dt.Rows

        items(i) = dr("PolicyNumber")

        items(i) = items(i).TrimStart("0"c)

        'items.SetValue(dr("PolicyNumber").ToString, i)

        i += 1

     Next

     Return items

  End Function

JavaScript:

       function initCascadingAutoComplete() {

           var symbolAutoComplete = $find('autoCompleteBehavior1');

           var numberAutoComplete = $find('autoCompleteBehavior2');

           numberAutoComplete.set_contextKey(symbolAutoComplete.get_element().value);

           symbolAutoComplete.add_itemSelected(cascade);

           // setup initial state of second flyout

           if (symbolAutoComplete.get_element().value) {

               numberAutoComplete.get_element().disabled = false;

           } else {

               numberAutoComplete.get_element().disabled = true;

               numberAutoComplete.get_element().value = "";

           }

       }

       function cascade(sender, ev) {

           var numberAutoComplete = $find('autoCompleteBehavior2');

           numberAutoComplete.set_contextKey(ev.get_text());

           numberAutoComplete.get_element().value = '';

           if (numberAutoComplete.get_element().disabled) {

               numberAutoComplete.get_element().disabled = false;

           }

       }  

       Sys.Application.add_load(initCascadingAutoComplete);

Design Source:

<cc1:AutoCompleteExtender ID="PolicySymbol" Enabled="true" TargetControlID="txtPolicySymbol" behaviorid="autoCompleteBehavior1"

                                MinimumPrefixLength="1"  ServicePath="AutoCompleteService.asmx"

                                Servicemethod="GetPolicySymbols" runat="server" CompletionInterval="100" CompletionSetCount="15">

  </cc1:AutoCompleteExtender>

  <cc1:AutoCompleteExtender ID="PolicyNumber" Enabled="true" TargetControlID="txtPolicyNumber"  BehaviorID="autoCompleteBehavior2"

                                MinimumPrefixLength="1"  ServicePath="AutoCompleteService.asmx"

                                Servicemethod="GetPolicyNumbers" runat="server" CompletionInterval="100" CompletionSetCount="15" EnableViewState="False">

  </cc1:AutoCompleteExtender>

Friday, November 21, 2008 3:57 PM by Mike

# re: Cascading AutoComplete

Hey Oran,

Please turn off caching on your autocomplete extender by setting the EnableCaching property:

EnableCaching="false"

Cheers,

Mike

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker