AutoCompleteExtender Client API

I previously posted about the format that a web service method has to be in for the AutoCompleteExtender to work.  This blog post focuses on some of the other capabilities of this extender.  In an example web method, I concatenate text and return the concatenation using LINQ as follows.

 [WebMethod]

public

 

 string[] GetTopSearchPhrases(string prefixText, int count) {

  SearchManager manager = SearchManager.GetManager();

 

 

  SearchPhraseCollection phraseCollection = manager.GetTopSearchPhrases(prefixText, count);

 

return (from p in phraseCollection select string.Format("{0} ({1} occurrence(s))"
    
, p.Phrase, p.ResultCount.ToString())).ToArray();

 

 

 

}

What I want to do is get the results as:

Microsoft .NET (21 occurence(s))
Microscopes (122 occurence(s))

And so on.  But, when the user selects an item in the list, I do not want the parenthesized text to appear in the textbox.  To trim this, I added a javascript event handler for the OnClientItemSelected event, that does this:

//Ensures that the parenthesized text isn't passed on

function

 

autoComplete_itemSelected(sender, e) {

 

 var text = e.get_text();

 

if (text.indexOf('(') > 0) {

  text = text.substring(0, text.indexOf(

 

'('));

 

if (text != null && text.length > 0)

 

    text = text.trimEnd();

 }

 

 

 var targetElement = sender.get_element();

 

 

 targetElement.value = text;

}

What this does is it gets the text of the item currently selected.  Then, if the text contains a parenthesis, it strips it out.  If the text happens to be not null, it's trimmed (to remove the extra spacing from " (" in the text; I'd rather programmatically trim than do a -1 just in case I change the formatting of the output string later and forget to change the accompanying event handler.

Lastly, the sender is the AutoCompleteExtender client class, and get_element() returns a reference to the target textbox.  The value is overridden (as the AutoCompleteExtender assigns the selected value to the textbox, but our event handler occurs after that assignment, wiping out any changes already performed).

You may wonder where get_text() came from; the event argument is AutoCompleteItemEventArgs, which has a get_text(), get_value(), and get_item() properties.  The latter is information about the item itself, whereas the first two contain the information about the items in the list.  You wouldn't know without the documentation what this event arg was.  I found this out by looking in the AJAX control toolkit code myself.  I could have put a debugger breakpoint in the code and figured it out that way as well.

 

 

Published Monday, August 04, 2008 6:12 AM by bmains
Filed under:

Comments

# Link Listing - August 3, 2008

Monday, August 04, 2008 12:27 AM by Christopher Steen

AJAX AutoCompleteExtender Client API [Via: bmains ] Code Camps Hartford CodeCamp Registration Now Open...

# Link Listing - August 3, 2008

Monday, August 04, 2008 12:28 AM by Christopher Steen

Link Listing - August 3, 2008

# Links for August 5th 2008 | .Net

Wednesday, August 06, 2008 1:45 AM by Links for August 5th 2008 | .Net

Pingback from  Links for August 5th 2008 | .Net

# 280zx Control, 280zx Taillight Car Parts

Thursday, May 20, 2010 5:58 PM by 280zx Control, 280zx Taillight Car Parts

Pingback from  280zx Control, 280zx Taillight Car Parts

# Columbia Used Capitol Service, Columbia Parts Gypsum Construction Handbook Drywall Tools

Pingback from  Columbia Used Capitol Service, Columbia Parts Gypsum Construction Handbook Drywall Tools

# L300 2 Accessories Strut, L300 1 Sell Automobile

Saturday, May 22, 2010 2:05 AM by L300 2 Accessories Strut, L300 1 Sell Automobile

Pingback from  L300 2 Accessories Strut, L300 1 Sell Automobile

# Dodge Omni Forum Pt Cruiser, Omni Parts Spark Plug Wires Dodge Dakota

Pingback from  Dodge Omni Forum Pt Cruiser, Omni Parts Spark Plug Wires Dodge Dakota

# 2 El Oto Honda Crv, Honda Crv Radiator Spark Plug Wires Ignition - 109.akemet.com

Pingback from  2 El Oto Honda Crv, Honda Crv Radiator Spark Plug Wires Ignition - 109.akemet.com

# B3500 Boiler Pressure Vessel Design, Principle Pb350 - 479.jordanbrandallamerican.com

Pingback from  B3500 Boiler Pressure Vessel Design, Principle Pb350 - 479.jordanbrandallamerican.com

# 1994 - 1993 @ Refurbished Lexus Lx470 Parts, 1998 Lexus Lx470 Starter - 201.ja3ra.com

Pingback from  1994 - 1993 @ Refurbished Lexus Lx470 Parts, 1998 Lexus Lx470 Starter - 201.ja3ra.com

# 1987 - 1991 @ Haszn?lt Nissan Micra, Ersatzteile Nissan Micra K11 - 494.rkwrh.com

Pingback from  1987 - 1991 @ Haszn?lt Nissan Micra, Ersatzteile Nissan Micra K11 - 494.rkwrh.com

# 1981 - 1984 @ 450slc Parts 1972 Mercedes Benz, Discount Mercedes Benz 450slc - 342.unlockiphone30.net

Pingback from  1981 - 1984 @ 450slc Parts 1972 Mercedes Benz, Discount Mercedes Benz 450slc - 342.unlockiphone30.net