Click here to Skip to main content
15,881,882 members
Articles / Web Development / ASP.NET

Databinding Web Forms to Objects as opposed to Datasets

Rate me:
Please Sign up or sign in to vote.
4.39/5 (10 votes)
20 May 20045 min read 78K   36  
A step-by-step walkthrough describing how to bind an ASP.NET web form to an object and object collection, as opposed to binding to datatables and fields.
using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;


namespace CL.DataBinding
{
	/// <summary>
	/// This object inherits from Component and thus can be placed on
	/// a web form at design time.  This class allows us to bind
	/// various web controls to our person object
	/// </summary>
	/// 
	public class PersonAdapter : System.ComponentModel.Component
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public PersonAdapter(System.ComponentModel.IContainer container)
		{
			///
			/// Required for Windows.Forms Class Composition Designer support
			///
			container.Add(this);
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		public PersonAdapter()
		{
			///
			/// Required for Windows.Forms Class Composition Designer support
			///
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// provides access to our person object so a web form
		/// can bind to the person's properties
		/// </summary>
		private Person person;
		public Person Person
		{
			get
			{
				return person;
			}
			set
			{
				person=value;
			}
		}


		/// <summary>
		/// We use this property to bind to one of our control's
		/// back color property
		/// </summary>
		public System.Drawing.Color FavoriteColor
		{
			get
			{
				switch (person.FavoriteColor.Color)
				{
					case "Blue":
						return System.Drawing.Color.Blue;
					case "Green":
						return System.Drawing.Color.Green;
					case "Red":
						return System.Drawing.Color.Red;
					default:
						return System.Drawing.Color.Black;

				}
			}
		}

		/// <summary>
		/// for check box binding
		/// </summary>
		public bool IsLongName
		{
			get
			{
				if (person.Name.Length>10)
					return true;
				else
					return false;
			}

		}

		/// <summary>
		/// binds to the text of our 'submit' button
		/// </summary>
		public string UpdateButtonText
		{
			get
			{
				return "Update " + person.Name;
			}
		
		}
		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}


		#region Component Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			components = new System.ComponentModel.Container();
		}
		#endregion
	}
}

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
New Zealand New Zealand
Chris Lennon lives in Auckland, New Zealand. He is married with two children. He is a software development manager, currently working on a distributed Windows Forms.NET application written in C#.

He is a fan of Extreme Programming (XP) and Rapid Application Development (RAD) methedologies. His interest is in creating a template-driven approach to facilitate the production of high quality windows and web applications, within a short development cycle.

Besides looking after his kids, Chris attends Christian Life Centre Auckland, a vibrant church in the heart of Auckland City.

Comments and Discussions