Thursday, January 22, 2009

Custom AutoComplete 5: Build an additional row on the top of AutoComplete List

Before anything, please check this(

We know we can customize AutoComplete. In this image, we can achieve the red background, Font style and seting some shining picture as the background. But one thing we need to notice is that there is an arrowhead on the top of row. So we have to make an additional row for it. And this row is not an item in the list; mouse event should be unavailable here.

Let code speak anything:

In this sample, you can define autocomplete_firstRow css style. In this case, you can define a background image in firstrow.



After you create an AutoComplete on the page, the below JavaScript will be worked of it. It will create an addition row on the top of AutoComplete. Suppose that the behaviorID of AutoComplete is "AutoCompleteEx".

<script type="text/javascript">

        function ClientPopulated() {
            var comletionList = $find("AutoCompleteEx").get_completionList();

            var firstrow=document.createElement("div");
            firstrow.className = "autocomplete_firstrow";

            comletionList.insertBefore(firstrow, comletionList.firstChild);



        function pageLoad() {
            var autoc = $find("AutoCompleteEx");
//redefine highlightitem function
            autoc._highlightItem = function(item) {

                var children = autoc._completionListElement.childNodes;
                if (item === children[0])

                for (var i = 0; i < children.length; i++) {
                    var child = children[i];
                    if (child._highlighted) {
                        if (autoc._completionListItemCssClass) {
                            Sys.UI.DomElement.removeCssClass(child, autoc._highlightedItemCssClass);
                            Sys.UI.DomElement.addCssClass(child, autoc._completionListItemCssClass);
                        } else {
                            if (Sys.Browser.agent === Sys.Browser.Safari) {
                       = 'white';
                       = 'black';
                            } else {
                       = autoc._textBackground;
                       = autoc._textColor;
                        autoc.raiseItemOut(new AjaxControlToolkit.AutoCompleteItemEventArgs(child, child.firstChild.nodeValue, child._value));

                // Highlight the new item
                if (this._highlightedItemCssClass) {
                    Sys.UI.DomElement.removeCssClass(item, this._completionListItemCssClass);
                    Sys.UI.DomElement.addCssClass(item, this._highlightedItemCssClass);
                } else {
                    if (Sys.Browser.agent === Sys.Browser.Safari) {
               = 'lemonchiffon';
                    } else {
               = 'highlight';
               = 'highlighttext';

                item._highlighted = true;

                this.raiseItemOver(new AjaxControlToolkit.AutoCompleteItemEventArgs(item, item.firstChild.nodeValue, item._value));


//redefine settext function
            autoc._setText = function(item) {
            var children = autoc._completionListElement.childNodes;
                if (item === children[0])
                var text = (item && item.firstChild) ? item.firstChild.nodeValue : null;


                var element = autoc.get_element();
                var control = element.control;
                if (control && control.set_text) {
                    $common.tryFireEvent(control, "change");
                else {
                    element.value = text;
                    $common.tryFireEvent(element, "change");
                autoc.raiseItemSelected(new AjaxControlToolkit.AutoCompleteItemEventArgs(item, text, item ? item._value : null));

                autoc._currentPrefix = autoc._currentCompletionWord();




Anonymous said...

thanks. this is the best tutorial i have seen on the more advanced autocomplete topic.

i have noticed there is perhaps a bug with autocomplete. if i enter a text character (say letter 'a' for example), then when the results are displayed, if i choose to click away from the results and the list gets hidden, then if i try and enter that same character 'a' again, the list wont display unless i enter a different character.

is there a solution for this?

Vince Xu said...

I experience this issue many times. My suggestion is we can define the onblur event of TextBox. In this event, you can re-define to leave the focus from Textbox.
BTW, this issue is due to the TextBox doesn't know it is lost the focus and get it back again. So we can make use of javascript to assign it.

Anonymous said...

i have noticed there is perhaps a bug with autocomplete.


Your movies on demand

rinram said...

Please post the CSS for autocomplete extender ..Thanks in advance