Monday, December 21, 2009

Auto complete Extender with Dropdown Like binding

<ajaxtoolkit:autocompleteextender behaviorid="AutoCompleteEx" id="autoComplete1" onclientitemselected="IAmSelected" runat="server" servicemethod="GetCompletionListKeyValuePair" servicepath="~/Services/AutoComplete.asmx" targetcontrolid="myTextBox">
</ajaxtoolkit:autocompleteextender>


function IAmSelected( source, eventArgs ) {
alert( " Key : "+ eventArgs.get_text() +" Value : "+eventArgs.get_value());
}

[WebMethod]

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 items = new List(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);

items.Add(AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(prefixText + c1 + c2 + c3, i.ToString()));

}

return items.ToArray();

}

No comments: