views:

611

answers:

4

I am making an app (asp.net/c#) that will autosuggest a couple fields that users type in. Each user will end up building their own auto-suggest list. Every time they add an item, if it's a new word, it will get added to their list of auto-suggests, just like gmail.

I was wondering how most people go about this? Making a call to the server for every key-press doesn't seem very efficient? Should I make a huge xml file with one entry for each user? An xml file for each user? how would I cache this to make it efficient?

All sorts of questions, but what I am basically looking for is best practices. Thank you.

+1  A: 

I would store the values in a database (where you are storing their user profiles maybe), and pull the values into ASP.NET Session when the user logs in. Then just use the values from the Session for the auto-complete values. This way you will only have to hit the database once when they log in.

mc2thaH
this is exactly what I did, and it works awesome!
naspinski
A: 

Chuck Norris doesn't write code. He stares at a computer screen until he gets the program he wants.

RJ Russell
+1  A: 

Chuck Norris recommends, depending on your JavaScript framework, that you can build client-side checks before making a server call to see if there are a given number of characters or words written in the field. The XML file should be dynamically generated with the auto-suggest words for a given user based on the information entered by that user in the database.

Depending on the application that you are trying to build it may be easier to perform full-text SQL queries on specific database fields. For performance you could probably build a cookie that will be cached by the clients' browser and have your application check for changes or check to see if it exists before performing a full database query.

Chuck does not recommend using session variables because you would degrade the performance of your application because it has to keep those auto-suggest values in memory for each user.

Chuck would like to know if you need anything else before he surrenders my brain.

RJ Russell
I bet your the weakest person on this board... :)
naspinski
+1  A: 

Data Storage
You should store this information in a database. Create a table or two to hold this type of information per user per field.

Client-side Query
You can query the database from the client side (via AJAX) when the user starts to type in the text field. If you do this, it is considered best practice to wait until there are at least 3 (ish) characters in the field before querying the server for suggestions. This reduces the number of requests slightly and the traffic between the client and server.

I use this method for fields where there is a large number of possible auto-suggest hits.

Caching
If you use this method, you can optimize the process by caching the response in certain situations.

Assuming that the query is fired off only after 3 characters are in the text field, you can then cache the entire result. Any further characters added to the text box will not increase the number of results returned. So, you can just continue to filter down the cached results. If, however, the user deletes a character, you can refresh the cache to make sure that you have the most appropriate auto-suggest hits available.

Server-side Preloading
On the server-side, gather all of this data and send it along with the page request. (You can push it down as a javascript array somewhere that can be referenced in the onchange event of the text field. This way, you don't have to make any AJAX calls.

This appears to be better for fields where there will not be a large number of possible auto-suggest hits.

EndangeredMassa