Adding Auto Suggest Box in the search Box

Adding Auto suggest box using Ajax(Microsoft ASP.NET Ajax extension) was very easy. You need a web service (returning the list of words), the autocompleteextender to show the panel and a text box. The webservice is required to return an array list. The code to use the autocompleteextender is very simple
<ajaxtoolkit:autocompleteextender runat="server"
id="AutoCompleteExtender1" enabled="true"
minimumprefixlength="1" servicepath="~/SuggestionService.asmx"
servicemethod="GetAllNames"
targetcontrolid="txtSearch" />
Here MinimumPrefixLength is the number of words after which the the suggestion will be shown. Service path is the path to the web service and service method is the webmethod that will be used. The target control ID is the id of the textbox. That Is the syntax of the extender control. We also have to make a web service. I decided to keep the values in a array list . The web service is like this
SuggestionService.asmx

     [WebMethod]
    [System.Web.Script.Services.ScriptMethod]
    public string[] GetAllNames(string prefixText, int count)
    {
        ArrayList filteredList = new ArrayList();
        string s2 = "\n";
        char[] ch = s2.ToCharArray();
        string[] names ={ "India", "UK", "US", "China", "Nepal" };

        foreach (string name in names)
        {
            if (name.ToLower().StartsWith(prefixText.ToLower()))
            {
                filteredList.Add(name);
            }
        }
        return (string[])filteredList.ToArray(typeof(string));
    }
Reactions

Post a Comment

1 Comments

  1. thanks dude its a good one......
    http://trickgeek.blogspot.com/

    ReplyDelete

Please do not post any spam link in the comment box😊

Emoji
(y)
:)
:(
hihi
:-)
:D
=D
:-d
;(
;-(
@-)
:P
:o
:>)
(o)
:p
(p)
:-s
(m)
8-)
:-t
:-b
b-(
:-#
=p~
x-)
(k)

Close Menu