Friday, October 12, 2007 9:58 AM kevinisom

Update to Ajax control toolkit breaks autocomplete

I recently updated an application to the latest version of the Ajax Control Toolkit and my AutoComplete Extender broke. It was returning the list fine. but in the dropdown the only values showed were undefined. Not cool. But after some research I changed the web service that returns the values to use the new AutoComplete Item

First Version

 

 1: [WebMethod]
 2: public string[] AutoComplete( string prefixText, int count )
 3: {
 4:  List<string> results = new List<string>(); 
 5:  IDataReader reader = DataLayer.AutoCompleteLookup(prefixText).GetReader(); 
 6:  while (reader.Read())
 7:  {
 8:  results.Add(reader[0].ToString());
 9:  }
 10:  
 11:  return results.ToArray();
 12: }

That worked fine, and I have a feeling that if the service wasn't returning numeric values like 01-145 that it might still be fine, but the service was returning values like 01-145 so I had to do this

 1: [WebMethod]
 2: public string[] AutoComplete( string prefixText, int count )
 3: {
 4:  List<string> results = new List<string>(); 
 5:  IDataReader reader = DataLayer.AutoCompleteLookup(prefixText).GetReader(); 
 6:  while (reader.Read())
 7:  {
 8:  results.Add(
 9:  AutoCompleteExtender.CreateAutoCompleteItem
 10:  (
 11:  reader[0].ToString(), 
 12:  reader[0].ToString()
 13:  )
 14:  );
 15:  }
 16:  return results.ToArray();
 17: }

After that changed the AutoComplete worked again.

Now that got me wondering what I could do with the AutoComplete Item that comes back. So I found a post by Phani Raj. The Extender Control has a property OnClientItemSelected (actually the extender has 9 OnClient events) so you could do something like this.

 1: <ajax:AutoCompleteExtender ID="AutoComlete" runat="server"
 2:  TargetControlID="lookupBox"
 3:  ServicePath="~/services/Lookup.asmx" 
 4:  ServiceMethod="AutoComplete" 
 5:  OnClientItemSelected="AutoComlete_Selected" />
 6:  
 7: <script language="javascript">
 8:  function AutoComlete_Selected( source, eventArgs ) {
 9:  $get("hiddenField").value = eventArgs.get_value();
 10:  //eventArgs also has .get_text() 
 11:  }
 12: </script>

Hope this helps someone

Filed under: , ,

Comments

# re: Update to Ajax control toolkit breaks autocomplete

Thursday, October 11, 2007 7:58 PM by Joe Chung

Did you try using reader.GetString(0) instead of reader[0].ToString()?

# re: Update to Ajax control toolkit breaks autocomplete

Friday, October 12, 2007 6:19 AM by kevinisom

The WebService was returning the correct results, the AutoComplete was returning the correct number of results to be selected, but for every result it was listed as undefined. The new code doesn't seem to like numeric based items.

# re: Update to Ajax control toolkit breaks autocomplete

Tuesday, October 16, 2007 8:07 PM by James W

Had exactly the same problem. Your fix saved the day. Thanks.

# re: Update to Ajax control toolkit breaks autocomplete

Monday, October 22, 2007 9:41 PM by jason

hello:

do you understand how to display two values(name and description) under the autocomplete dropdownlist?

can you teach me  ,thanks.

# re: Update to Ajax control toolkit breaks autocomplete

Thursday, October 25, 2007 2:09 PM by gaurab

thanks. i was having the same problem and now it works like a champ.

to jason, you can do:

results.Add(

AutoCompleteExtender.CreateAutoCompleteItem

(

reader[0].ToString() + " - " + reader["Desc"].ToString(),

reader["Name"].ToString() + " - " + reader["Desc"].ToString()

)

);

# re: Update to Ajax control toolkit breaks autocomplete

Thursday, November 01, 2007 2:28 AM by dimple

thanx kavin ..

it worked so beautifully.. i was trying this from last 2 dayz n cldnt get through bt using ur trick everything is workingso fine ..

thnx again

# re: Update to Ajax control toolkit breaks autocomplete

Tuesday, March 04, 2008 12:22 PM by juliett

thaks, it helped me

# re: Update to Ajax control toolkit breaks autocomplete

Thursday, April 03, 2008 9:43 AM by Bert

Thanks for that tip - been searching the web for this solution....

Thanks again

# re: Update to Ajax control toolkit breaks autocomplete

Monday, June 23, 2008 10:53 AM by Maheswaran

Can any body know how to populate auto complete text for the text box while clicking one button. If any body knows please forward it to mail id . It is urgent

Leave a Comment

(required) 
(required) 
(optional)
(required)