The auto complete extender comes as part of the AjaxControlToolkit, it’s a really neat little extension to a TextBox that allows a user to be given suggestions based on what they type.
It is currently restricted so that it only shows text and no more. I’ve made a couple of changes that allow you to pass arbitrary html, which makes things a like more interesting.
Ok, so in the first thing I need to be able to do is pass the html from the webservice. This bit is easy, all we need to do is create an overload to AutoCompleteExtender. CreateAutoCompleteItem method. That’s easy, just add the following method to the AutoCompleteExtender class:
public static string CreateAutoCompleteItem(string text, string value, string markup)
{
return new JavaScriptSerializer().Serialize(new Pair(text, new Pair(value, markup)));
}
Now we need to make sure that the html is displayed, this is done by the _update method in AutoCompleteBehavior.js. In this method there is a loop, this spins through each of the list items that comes back from the web service. Essentially I detect if html is supplied and if it is then use the html, rather than simply putting a text node in place.
The other change is to the _setText function, this must search through the parent nodes to find which one has the text and id values which we sent from our web service.
The two files are attached, take them and drop them into your project, all your old code should continue to work, but you could write a web service like this to display html:
Public Function GetPersonList(ByVal prefixText As String, ByVal count As Integer) As String()
Dim table As DataSet
table = Database.Fill(“PersonSearch”, New SqlParameter() { _
Database.CreateParameter(“@Name”, SqlDbType.NVarChar, ParameterDirection.Input, prefixText), _
Database.CreateParameter(“@Count”, SqlDbType.Int, ParameterDirection.Input, count)})
Dim list(table.Tables(0).Rows.Count – 1) As String
Dim html As String
For i As Integer = 0 To list.Length – 1
html = String.Format(“
“, _
table.Tables(0).Rows(i)(“Forename”), _
table.Tables(0).Rows(i)(“Surname”))
list(i) = AutoCompleteExtender.CreateAutoCompleteItem(String.Format(“{0} {1}”, table.Tables(0).Rows(i)(“Forename”), table.Tables(0).Rows(i)(“Surname”)), _
String.Format(“{0}”, table.Tables(0).Rows(i)(“PersonId”)), _
html)
Next
Return list
End Function
That’s it, you now have a list of people, with a picture next their name, which is nice.
I hope this helps someone.
Here’s the complete modified AutoCompleteBehavior.js: http://www.mediafire.com/?4ztezjxnfnl
and the modified AutoCompleteExtender.cs: http://www.mediafire.com/?5idmqczandm

7 comments
Comments feed for this article
11 March 2009 at 10:34 pm
Rahul
Thanks aton…. u are super cool
13 March 2009 at 9:50 am
Rahul
Hi! seen ur example and implemented it but OnMouseOver event is not working… can u tell me y?????
13 March 2009 at 10:01 am
ilivewithian
Can you provide a short example where it’s not working?
10 September 2009 at 12:57 am
allen’s codeSource » Blog Archive » Display HTML in an AJAX autoCompleteExtender
[...] first found out about this from Rob White, aka ILIVEWITHIAN on The Old Sewing Factory. Credit goes to him, this is just an example of how I did it in [...]
10 September 2009 at 1:00 am
Allen Ringgold
Rob,
Thanks for this post. I used your methods recently and did a writeup of my implementation here, giving you credit throughout. Hope you don’t mind, but I linked back to this post and your .js file.
Thanks again!
-Allen
3 November 2009 at 6:23 pm
Joel Chretien
This should be added to the framework itself.
5 November 2009 at 10:34 am
ilivewithian
Thanks Joel, feel free to suggest it to the ajax toolkit guys.