Click here to Skip to main content
15,881,803 members
Articles / Web Development / HTML

AJAX AutoComplete/AutoSuggest TextBox

Rate me:
Please Sign up or sign in to vote.
4.86/5 (51 votes)
2 Oct 20074 min read 879.8K   7K   147  
An article on AJAX AutoSuggest control
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Web.UI.HtmlControls;

public partial class MultipleColumns : System.Web.UI.Page
{
	#region Session Variables
	private DataTable _dtProducts
	{
		get { return (DataTable)Session["_dtProducts"]; }
		set { Session["_dtProducts"] = value; }
	}
	#endregion

	protected void Page_Load(object sender, EventArgs e)
    {
		if (!this.IsPostBack)
		{
			FillDataTable();
		}
    }
	
	protected override void OnInit(EventArgs e)
	{
		base.OnInit(e);
		this.asbProduct.TextChanged += new Anthem.AutoSuggestBox.TextChangedEventHandler(asbProduct_TextChanged);
	}


	#region FillDataTable
	/// <summary>
	/// This method fills the datatable that is used in this example
	/// On a real world application you usually would use a database
	/// </summary>
	private void FillDataTable()
	{
		_dtProducts = new DataTable();
		_dtProducts.Columns.Add(new DataColumn("ProductID", typeof(int)));
		_dtProducts.Columns.Add(new DataColumn("ProductName", typeof(string)));
		_dtProducts.Columns.Add(new DataColumn("Price", typeof(decimal)));

		DataRow row = _dtProducts.NewRow();
		row["ProductID"] = 1;
		row["ProductName"] = "Toshiba Satellite P105-S6114 17\"";
		row["Price"] = 1249.99 ;
		_dtProducts.Rows.Add(row);

		row = _dtProducts.NewRow();
		row["ProductID"] = 2;
		row["ProductName"] = "Toshiba Qosmio G35-AV650 17\"";
		row["Price"] = 2119.98;
		_dtProducts.Rows.Add(row);

		row = _dtProducts.NewRow();
		row["ProductID"] = 3;
		row["ProductName"] = "Samsung 225BW Black 22\" Widescreen LCD Monitor";
		row["Price"] = 324.98;
		_dtProducts.Rows.Add(row);

		row = _dtProducts.NewRow();
		row["ProductID"] = 4;
		row["ProductName"] = "Acer AL2223Wd 22\" Widescreen LCD Monitor";
		row["Price"] = 313;
		_dtProducts.Rows.Add(row);

		row = _dtProducts.NewRow();
		row["ProductID"] = 5;
		row["ProductName"] = "Apple 30 GB iPod video Black";
		row["Price"] = 237.98;
		_dtProducts.Rows.Add(row);

		row = _dtProducts.NewRow();
		row["ProductID"] = 6;
		row["ProductName"] = "Zune 30 GB Digital Media Player";
		row["Price"] = 249.99;
		_dtProducts.Rows.Add(row);

		row = _dtProducts.NewRow();
		row["ProductID"] = 7;
		row["ProductName"] = "Diamond Stealth ATI 9550 AGP 128MB Video Card";
		row["Price"] = 43.99;
		_dtProducts.Rows.Add(row);

		row = _dtProducts.NewRow();
		row["ProductID"] = 8;
		row["ProductName"] = "World Of Warcraft Expansion: Burning Crusade";
		row["Price"] = 32.99;
		_dtProducts.Rows.Add(row);

		row = _dtProducts.NewRow();
		row["ProductID"] = 9;
		row["ProductName"] = "HP LaserJet 1022 Printer ";
		row["Price"] = 242.99;
		_dtProducts.Rows.Add(row);
		
	}
	#endregion
		
	/// <summary>
	/// This event handler is used to fill the suggest list that appears as the user types
	/// Usually you would query a database, but for the sake of simplicity we are querying an in-memory datatable
	/// </summary>	
	void asbProduct_TextChanged(object source, Anthem.AutoSuggestEventArgs e)
	{
		//Creates a dataview of the session's datatable		
		DataView dv = new DataView(_dtProducts);
		//Filters the datatable based on the CurrentText property
		dv.RowFilter = string.Format("ProductName LIKE '%{0}%'", e.CurrentText);
		//Sets the dataview as the control's datasource				
		asbProduct.DataSource = dv;
		asbProduct.DataBind();
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior) Intelligent Coder
Canada Canada
I've been developing .NET enterprise applications since 2000.

I am originally from Rio de Janeiro and I am currently working at http://www.intelligentcoder.com in Ontario.

I also have my own startup where we offer client intake forms.

Comments and Discussions