I have an AutoCompleteExtender from the Ajax Control Toolkit. I need to have a heading in the dropdown list that shows how many items found, but it should not be selectable as an item.
I have tried this using jQuery, but even when I just add as a div, it is still selected as an item into the text box when I click on it:
function clientPopulated(sender, e) {
var completionList = $find("AutoCompleteEx").get_completionList();
var completionListNodes = completionList.childNodes;
for (i = 0; i < completionListNodes.length; i++) {
completionListNodes[i].title = completionListNodes[i]._value.split(':')[2];
}
var resultsHeader;
if(completionListNodes.length==1000)
resultsHeader = 'Max count of 1000 reached.<br/>Please refine your search.';
else if(completionListNodes.length>0)
resultsHeader = completionListNodes.length + ' hits.';
else
resultsHeader = msg_NoObjectsFound ;
jQuery(completionListNodes[0]).before('<div>' + resultsHeader + '</div>');
}
Add
OnClientItemSelected
andOnClientShowing
events handlers and try script below:Added: you even can do this without
OnClientItemSelected
handler:We can give a better answer if you post the output html of your autocomplete control. Anyway if its a dropdown control;
The answer by Yuriy helped me in solving it so I give him credit although his sollution needed some changes to work.
First of all, the
clientShowing
event (mapped by settingOnClientShowing = "clientShowing"
in the AutoExtender control) is executed on initialization. Here we override the_setText
method to make sure nothing happens when clicking on the header element. I have used the overriding idea from Yuriy's answer that really did the trick for me. I only changed to check on css class instead of aref
attribute value.So then we need to add the header element to the top of the list. This has to be done in the
clientPopulated
event (mapped by settingOnClientPopulated = "clientPopulated"
in the AutoExtender control). This event is executed each time the search results have been finished populated, so here we have the correct search count available.I have also created a new css class to display this properly. I have used
!important
to make sure this overrides the mousover style added from the AutoExtender control.