Click here to Skip to main content
Click here to Skip to main content
Articles » Web Development » Ajax » Controls » Downloads
 
Add your own
alternative version

AJAX AutoComplete/AutoSuggest TextBox

, 2 Oct 2007
An article on AJAX AutoSuggest control
autosuggestbox.zip
AutoSuggestBox
AutoSuggestBox
AutoSuggest
AutoSuggest.suo
AutoSuggest
Anthem.AutoSuggest.csproj.user
bin
Debug
Anthem.AutoSuggest.dll
Anthem.dll
Release
Anthem.AutoSuggest.dll
Anthem.AutoSuggest.pdb
Anthem.dll
Anthem.pdb
includes
vssver2.scc
Properties
Example
App_Code
App_Data
Bin
Anthem.AutoSuggest.dll
Anthem.AutoSuggest.pdb
Anthem.dll
Anthem.pdb
example.zip
Example
App_Code
App_Data
sample.zip
Bin
Anthem.AutoSuggest.dll
Anthem.AutoSuggest.pdb
Anthem.dll
Anthem.pdb
sourcefiles.zip
AutoSuggest
Anthem.AutoSuggest.csproj.user
bin
Debug
Anthem.AutoSuggest.dll
Anthem.dll
includes
vssver2.scc
Properties
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 DynamicControl : 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);

		Anthem.AutoSuggestBox asbFavoriteBand = new Anthem.AutoSuggestBox();
		asbFavoriteBand.ID = "asbFavoriteBand";
		asbFavoriteBand.DataKeyField = "ID"; 
		asbFavoriteBand.TextBoxDisplayField="Name";
		asbFavoriteBand.AutoCallBack=true;
		asbFavoriteBand.ItemNotFoundMessage = "Band not found!";
		asbFavoriteBand.TextChanged += new Anthem.AutoSuggestBox.TextChangedEventHandler(asbFavoriteBand_TextChanged);
		asbFavoriteBand.SelectedValueChanged += new Anthem.AutoSuggestBox.SelectedValueChangedHandler(asbFavoriteBand_SelectedValueChanged);
		asbFavoriteBand.ItemDataBound += new Anthem.AutoSuggestBox.ItemDataBoundHandler(asbFavoriteBand_ItemDataBound);
		pnDynamic.Controls.Add(asbFavoriteBand);
	}

	void asbFavoriteBand_ItemDataBound(object source, Anthem.AutoSuggestItemEventArgs e)
	{
		Band band = (Band)e.Item.DataItem;
		Label lbl = new Label();
		lbl.Text = band.Name;
		e.Item.Controls.Add(lbl);
	}

	#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)
	{
		Anthem.AutoSuggestBox asbFavoriteBand = (Anthem.AutoSuggestBox)source;
		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

About the Author

Cassio Mosqueira
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.

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 2 Oct 2007
Article Copyright 2007 by Cassio Mosqueira
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid