Friday, October 31, 2008

Custom AutoComplete 1: Mutiple rows message for each Item

AjaxControlToolkit AutoComplete will pop up the result list according to what you typed. Each items of result list contains one-line text. When we type like "Vin", it will return "Vince", "Vincent", "Vint" in the result list. But if we want to append some detail to an additional line within the an item, how we can get that? For example, When we type "Vin", we hope it will result the related name and age in an additional line. When we select the item, it will return just name as the value. (See the thumbnail)

<form id="form1" runat="server">
<ajaxToolkit:ToolkitScriptManager runat="server" ID="ScriptManager1" />
<asp:TextBox runat="server" ID="myTextBox" Width="300" autocomplete="off" />
runat="server" OnClientPopulated="dd" OnClientItemSelected="itemSelected"
DelimiterCharacters=";, :">

<script type="text/javascript">
function itemSelected(ev)
var index=$find("AutoCompleteEx")._selectIndex;
var dd=$find("AutoCompleteEx").get_completionList().childNodes[index]._value;
function dd()
var comletionList=$find("AutoCompleteEx").get_completionList();

var _value=comletionList.childNodes[i]._value;
comletionList.childNodes[i]._value=_value.substring(_value.lastIndexOf('|')+1);// parse id to _value



WebMethod in AutoComplete.asmx:

public string[] GetCompletionList(string prefixText, int count)
    if (count == 0)
        count = 10;

    if (prefixText.Equals("xyz"))
        return new string[0];

    Random random = new Random();
    List<string> items = new List<string>(count);

    for (int i = 0; i < count; i++)
        char c1 = (char)random.Next(65, 90);
        char c2 = (char)random.Next(97, 122);
        char c3 = (char)random.Next(97, 122);
        int id = i;
        int age = random.Next(18, 70);
        items.Add(prefixText + c1 + c2 + c3 +"|"+age.ToString()+"|"+id.ToString());

    return items.ToArray();

ID, Name and Age will be returned from web method to the client. JavaScript will change the format of result and set the value as ID. When you select the item, the value will be as the text of TextBox.

In this way, we can even append an image in font of each item. I will intoduce about "AutoComplete with Image" next time.


Paul said...


I am trying to adapt your code to create a custom Auto Complete Extender that reads both a name and an ID from MySql. I need to select the ID and then insert it into another DB table. I want to allow the user to select multiple items from a database once they start typing. Can you please help me out with some example code?

evschu said...

Very nice - thanks for sharing. The only issue that I have is that the items in the list don't get highlighted if I mouse over the image or text. Only if I mouse over the white space to the right of the text will the items highlight? If I take out the html table then it works, but that messes up the formatting. Is there a work around (or perhaps something in your css styles that I'm missing).

Thanks again!

Anonymous said...

How to select multiple values with their ID ?

Example: John, David, Charlie

raj kumar said...

this code not working get
500 (Internal Server Error)