![]() |
Web Development »
ASP.NET »
Howto
Intermediate
Adding 'Google Suggest' functionality to an ASP.NET applicationBy Niraj SharmaShows how to add 'Google Suggest' functionality to an ASP.NET application. |
C#, Javascript, XML.NET 1.1, Win2K, WinXP, ASP.NET, WebForms, Ajax, VS.NET2003, Dev
|
|
Advanced Search |
|
|
||||||||||||||||||

The purpose of this article is to show how to use the free AutoSuggestBox control to add 'Google Suggest' functionality to your ASP.NET application. There are plenty of articles available on the internet that explains how to do it, but there is usually just a lot of theory and not much code that you can easily add to your application. AutoSuggestBox encapsulates all the complex functionality and only requires developers to specify the data source for loading the auto-suggest menu.
Have you seen Google Suggest? It is an application created by Google that demonstrates the power of AJAX. It is really amazing to see it for the first time, but in a little while, the notoriety wears off and most developers forget about it. While many people acknowledge its 'coolness' factor, they don�t know how to integrate it with other web applications.
The first time I saw this control, I came away with the same feeling. What an awesome idea, but I have no use for it. Several months ago, I started working on a travel application called 'Travelope' (see here). When working on the airfare search form, I was thinking of a simple interface for selecting FROM and TO locations. I considered using a combo box, but there were 1000s of cities, so the form would take a long time to load. Another option would be to use a text box with a link to popup a city lookup window. That would require users to open a reference window every time they forgot the spelling, so that would not be a great option either. I started looking at other travel sites for ideas. Many of these sites were using 'Google Suggest'-like controls for the purpose of selecting cities and airports. I thought that would be perfect for 'Travelope', too. And that was how AutoSuggestBox was born.
AutoSuggestBox is a custom control written in C# that makes it simple to add 'Google Suggest'-like functionality to your web applications. It supports C# and VB.NET and works in Internet Explorer as well as in Firefox. This control utilizes AJAX to retrieve data without refreshing the whole page. Developers can use CSS to adjust the look and feel of the control on the web page.
Download the appropriate AutoSuggestBox package from here. There are four files that you need to add to your application to use AutoSuggestBox.
First, add a reference to 'AutoSuggestBox.dll'. Then create a sub-directory 'asb-includes' under the root of your web application. Copy the other three files into the new sub-directory.
More detailed instructions are available here.
When the user starts typing in the AutoSuggestBox, you need to provide a data source for the suggested values. The system allows for as many data sources as required by your application. For example, you can have one data source for the 'City' suggest box, another for the 'Airport'.
To add a data source, you need to modify the 'GetAutoSuggestData.aspx' file. By default, the class implements the "City" datasource. "LoadMenuItems" contains a switch statement for loading different types of data:
private ArrayList LoadMenuItems(string sDataType, string sKeyword)
{
ArrayList aMenuItems;
switch(sDataType)
{
case "City":
aMenuItems=LoadCities(sKeyword);
break;
default:
throw new Exception("GetAutoSuggestData Type '" +
sDataType + "' is not supported.");
}
return aMenuItems;
}
The 'LoadCities' method retrieves the suggested cities from the database according to the characters typed in by the user. It loads the top 10 cities starting with a specified keyword. Then it generates an array of ASBMenuItems and fills it with the results from the database.
private ArrayList LoadCities(string sKeyword)
{
ArrayList aOut=new ArrayList();
string sConnString="Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=c:\\databases\\AutoSuggestBox_Demo.mdb;" +
"User Id=admin;Password=;";
OleDbConnection oCn=new OleDbConnection(sConnString);
string sSql="SELECT TOP 10 tblCity.Name as CityName, " +
"tblCity.Code as CityCode, " +
"tblCountry.Name as CountryName, " +
"tblState.Name as StateName " +
" FROM (tblCity INNER JOIN tblCountry" +
" ON tblCity.CountryID=tblCountry.ID) " +
" LEFT OUTER JOIN tblState" +
" ON tblCity.StateID=tblState.ID " +
" WHERE (tblCity.Name LIKE '" +
sKeyword.Replace("'", "''") + "%') " +
" ORDER BY tblCity.Name";
OleDbCommand oCmd = new OleDbCommand(sSql, oCn);
oCn.Open();
OleDbDataReader oReader = oCmd.ExecuteReader();
string sCity;
string sCityCode;
string sState;
string sCountry;
string sLabel;
ASBMenuItem oMenuItem;
while (oReader.Read())
{
//Build label using City, Country & State
sCity =oReader.GetString(0);
sCityCode =oReader.GetString(1);
sCountry =oReader.GetString(2);
if (oReader.GetValue(3)==System.DBNull.Value)
sState="";
else
sState=oReader.GetString(3);
sLabel=sCity;
//Append either state or country
if (sState != "")
sLabel+=", " + sState;
else
sLabel+=", " + sCountry;
oMenuItem=new ASBMenuItem();
oMenuItem.Label=sLabel;
oMenuItem.Value=sCityCode;
aOut.Add(oMenuItem);
}
oReader.Close();
oCn.Close();
return aOut;
}
If you need to add other data sources, just add more case statements to the LoadMenuItems function.
<%@ Register TagPrefix="Custom" Namespace="ASB" Assembly="AutoSuggestBox" %>
<Custom:AutoSuggestBox id="Control ID"
DataType="Supported Data Type" runat="server"
CssClass="Text Box Style"
MaxLength="Max chars in TextBox"
Columns="Num of visible chars"/>
For example:
<Custom:AutoSuggestBox id="asbCity" DataType="City"
runat="server" CssClass="FormCtrlStyle"
MaxLength="100" Columns="30"/>
DataType attribute has been implemented in 'GetAutoSuggestData.aspx.cs'. See the previous section.
AutoSuggestBox tag: <Custom:AutoSuggestBox id="asbCity"
DataType="City" runat="server" CssClass="FormCtrlStyle"
MaxLength="100" Columns="30"
ResourcesDir="/WebApp/AutoSuggestBox"/>
Note: By default, the ResourcesDir is "/asb_includes".
When the user starts typing text in the control, JavaScript handles the OnKeyUp, OnKeyDown and OnKeyPress events. Then, AJAX is used to contact the server side with every click, to retrieve a list of suggested entries.
So the flow of events is as follows:
AutoSuggestBox control.
XmlHttpRequest to the GetAutoSuggestData page on the server.
In this article, we demonstrated how simple it is to add 'Google Suggest' functionality to your web applications using the free AutoSuggestBox control.
Happy programming!!!
AutoSuggestBox.
AutoSuggestBox control.
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 1 Dec 2005 Editor: Smitha Vijayan |
Copyright 2005 by Niraj Sharma Everything else Copyright © CodeProject, 1999-2009 Web17 | Advertise on the Code Project |