providing suggestion of words in thesearch box from a list in the text File

Hi

So I have continued added more and more Ajax to the site. Now I have added Auto Suggest Box to the site. The search box on the left has been made into an auto suggest box. Although the numbers of words at present are not very high but with the word manager ready it should be a matter of time when I will have many words to the auto Suggest in the search box.

Adding Auto suggest box using Atlas (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

<Atlas:autocompleteextender id="AutoCompleteExtender1" runat="server">

<Atlas:AutoCompleteProperties 

Enabled="True"

MinimumPrefixLength="1"

ServicePath="WebServiceName.asmx"

TargetControlID ="NameofTextBox"

ServiceMethod="GetAllNames" />

            </Atlas:autocompleteextender>

 
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 text file. (I did not felt the requirement of keeping the value in the database. More over it is not appropriate for the application point of view to query database whenever someone types a word in the search box.

The web service is like this

[WebMethod]

public string[] GetAllNames(string prefixText, int count)

{
  ArrayList filteredList = new ArrayList();
  string s2 = "\n";
  char[] ch = s2.ToCharArray();
  string[] names = Utils.ReadFile(Server.MapPath("~/PathOfTextFile.txt")).Split(ch);

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

Here I am picking up values from a text File. Utils.ReadFile method returns the values the text File.I am storing each word in a new line and hence splitting the values of the newline character. After that the work is very simple. To take the values and filter them on the given word and pass back the array of the filtered values.

Hope this helps
Thanks
Vikram

P.S. I know reading from the file also puts a lot of load on the application. Hence will be caching the values. Will post that in another post.


Share this post   Email it

Feedback

Posted on 11/19/2006 11:30:18 PM

hi
how can I do that with ASP.NET AJAX Toolkit?
I can`t find autocompleteextender there :(

Posted on 11/28/2006 7:06:20 AM

AutoCompleteExtender is in the Value-add CTP (http://www.microsoft.com/downloads/details.aspx?FamilyId=8A3FD0DD-D75E-4249-86DA-3D4AAC649652&displaylang=en) It will be moved to the control toolkit very soon.

Posted on 1/3/2007 5:49:20 AM

vikram, please take out this changing background colors. it is really annoying. you have a good blog, do not include all this fancy stuff.

Posted on 1/3/2007 8:25:33 AM

Hi

I have taken your suggestion and am going to remove this in a few days. I added this when I wanted to show how we can add animation to the site using atlas

Posted on 1/9/2007 5:08:58 AM

thanx for sharing information .... its relly cool to use autocompleteextender in my project

Posted on 1/24/2007 8:19:01 AM

I have seen your work, pretty Gud,
I too need such a work in my work bu I am unable to do it i want to get list of employees ie an employee search,Can u provide source code (any sample will do) for help....

Posted on 2/21/2007 10:58:21 PM

wondering if we need to give the web reference in the aspx page here or just the specification in service path will do

Posted on 2/22/2007 1:34:35 AM

Hi ibtesam

The specification in the service path will do the job

Posted on 3/11/2007 7:35:52 AM

Hello,
In this article picking values from file, BUT i want to pick all values from the DATABASE.

How to do that? It's Urgent!!

Posted on 3/11/2007 9:04:54 PM

I dont think that should be a problem. Instead of reading the file from a file you can get a database field and pass that back or u can use a xml file or anything that stores the data.

In my example get all the values from the database and add them in the filteredList through its add method. and pass it back

Posted on 3/12/2007 6:13:45 AM

How to insert Image in autocomplete dropdown menu item

Posted on 3/12/2007 7:21:51 AM

Hello Vikram
Your artcle is very good.. I have done the picking values from database. But i want to add some more in that...

In this autocomplete dropdown, i want select First dropdowm item By default & when i press TAB key it will go to TextBox. Here only Enter Key works. How to do that?

Posted on 3/12/2007 7:11:43 PM

Hi Nitin,

I dont think we can have images in the Auto complete.

Posted on 6/23/2007 9:53:37 PM

I am getting an error:
Error 36 The name 'Utils' does not exist in the current context.

Posted on 6/24/2007 3:07:35 AM

Util is one of the class that I have with utility functions. You need to create a small function that will read the file and replace the Utils.ReadFile function with your function

Posted on 7/27/2007 8:59:50 AM

Hi,
Simple to understand posting.

Is there a way to pass another parameter(or control value) to the webservice using auto controller on a webform?

Thanks.

Posted on 7/27/2007 9:04:29 AM

Hi Emmi,

Sorry to say but you cannot pass anymore parameter to the webservice

Posted on 8/21/2007 1:43:42 PM

Hi,

how to create that utils function..please mail me the code

Posted on 10/4/2007 4:34:21 AM

Hi its too good But i want to pass one value to the webservice How can i Tell me And Send Some sample ATLAS Codes To me i want to learn this Technology

Posted on 2/12/2008 2:24:50 AM

i am unable to run application using autocompleteextender in my machine. i have ie 6.0 and dotnet 2.0. when i try running after i include scriptManager and autocompleteextender shows javascript error saying "Sys in undefined".

thanks for your help
kumar

Posted on 3/5/2008 10:43:41 PM

Hi,
How can i return more than one values. like name and id.
Please reply asap. I will be highly grateful to u.
Thanks,
Jagadish

Posted on 3/6/2008 6:22:57 AM

hi Jagadish Manna,

You can concatenate the value in one string and pass it back. I dont think there is any other way

Posted on 4/7/2008 10:56:53 AM

how can i create like in www.naukri.com. what is some special in the naukri's search box is when we type any character it makes that character bold in the list. just check out the www.naukri.com and type in jobsearch box ;)

Posted on 5/29/2008 7:00:20 AM

GR8 work

Posted on 7/9/2008 3:20:28 AM

Hi vikram,

I'm also using the same ajaxtoolkit control autocomplete and it worked nice till I have implemented the url rewriting after that it always displaying a js error (syntax error)

Is there any way to do auto complete with url rewriting.

Thanks

Posted on 1/15/2009 4:24:35 AM

hi can u tell how to use two autocomplete textbox with different database values

Posted on 1/21/2009 8:12:02 AM

Im using framework3.5 but its not working....

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 

Copyright © 2006 - 2010 Vikram Lakhotia