Click here to Skip to main content
15,891,529 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 883.5K   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 ObjectBinding : System.Web.UI.Page
{
	#region Session Variables
	private List<Band> _bands
	{
		get { return (List<Band>)Session["_bands"]; }
		set { Session["_bands"] = value; }
	}
	#endregion

	protected void Page_Load(object sender, EventArgs e)
    {
		if (!this.IsPostBack)
		{
			FillCollection();
		}
    }
	
	protected override void OnInit(EventArgs e)
	{
		base.OnInit(e);
		this.asbFavoriteBand.TextChanged += new Anthem.AutoSuggestBox.TextChangedEventHandler(asbFavoriteBand_TextChanged);
		this.asbFavoriteBand.SelectedValueChanged += new Anthem.AutoSuggestBox.SelectedValueChangedHandler(asbFavoriteBand_SelectedValueChanged);
	}

	#region FillDataSource
	/// <summary>
	/// This method fills the generic list that is used in this example
	/// On a real world application you usually would use a database
	/// </summary>
	private void FillCollection()
	{
		_bands = new List<Band>();
		_bands.Add(new Band(1, "Dream Theater"));
		_bands.Add(new Band(2, "Pain of Salvation"));
		_bands.Add(new Band(3, "Savatage"));
		_bands.Add(new Band(4, "Iron Maiden"));
		_bands.Add(new Band(5, "Symphony X"));
		_bands.Add(new Band(6, "Angra"));
		_bands.Add(new Band(7, "Metallica"));
		_bands.Add(new Band(8, "System of a Down"));
		_bands.Add(new Band(9, "Whitesnake"));
		_bands.Add(new Band(10, "Van Halen"));
	}
	#endregion

	void asbFavoriteBand_SelectedValueChanged(object source, EventArgs e)
	{
		
	}

	/// <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 collection
	/// </summary>	
	void asbFavoriteBand_TextChanged(object source, Anthem.AutoSuggestEventArgs e)
	{
		asbFavoriteBand.DataSource = Select(e.CurrentText);
		asbFavoriteBand.DataBind();

	}

	private List<Band> Select(string searchString)
	{
		List<Band> list = new List<Band>();
		foreach(Band band in _bands)
		{
			if (band.Name.ToLower().Contains(searchString.ToLower()))
				list.Add(band);
		}
		return list;
	}
}

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