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.