Click here to Skip to main content
15,892,298 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 78.1K   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.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using CL.DataBinding;

namespace WebApplication1
{
	/// <summary>
	/// Summary description for _default.
	/// </summary>
	public class _default : System.Web.UI.Page
	{
		protected CL.DataBinding.PersonAdapter personAdapter1;
		protected System.Web.UI.WebControls.Button Button1;
		protected System.Web.UI.WebControls.TextBox TextBox3;
		protected System.Web.UI.WebControls.Label lblStatsHeader;
		protected System.Web.UI.WebControls.TextBox FirstName;
		protected System.Web.UI.WebControls.TextBox LastName;
		protected System.Web.UI.WebControls.CheckBox CheckBox1;
		protected System.Web.UI.WebControls.TextBox Textbox1;
		protected System.Web.UI.WebControls.DropDownList FavoriteColor;
		private System.ComponentModel.IContainer components;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			//we need a person object to be created to get the
			//ball rolling
			if (StateHelper.Person==null)
				StateHelper.Person=StateHelper.TestPerson;
			
			//if the page has been posted back then apply updates requested
			if (this.IsPostBack)
			{
				//pass form's Request name value collection to the
				//static Update method
				Person.Update(Request.Form);
			}

			//pass our person object to our adapter.  The various controls
			//are bound to this adapter
			this.personAdapter1.Person=StateHelper.Person;

			//this one line of code takes care of setting up the
			//entire web form including setting controls' text properties
			//back color etc etc
			this.DataBind();

			//the databinding should have taken care of this but
			//for some reason it ignores the SelectedValue binding
			//Oh well - we just do it ourselves then
			this.FavoriteColor.SelectedValue =StateHelper.Person.FavoriteColor.Color;

		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.components = new System.ComponentModel.Container();
			this.personAdapter1 = new CL.DataBinding.PersonAdapter(this.components);
			// 
			// personAdapter1
			// 
			this.personAdapter1.Person = null;
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#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