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

Web User Forms for ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.90/5 (52 votes)
18 Sep 2012CPOL12 min read 135.3K   4.5K   187  
User driven runtime dynamic ASP.NET Web Forms
// -- FILE ------------------------------------------------------------------
// name       : UserFormVisitor.cs
// project    : Itenso Web User Forms
// created    : Jani Giannoudis - 2008.10.30
// language   : c#
// environment: .NET 2.0
// copyright  : (c) 2008-2012 by Itenso GmbH, Switzerland
// --------------------------------------------------------------------------
using System;
using System.Web.UI;

namespace Itenso.WebUserForms.Controls
{

	// ------------------------------------------------------------------------
	public abstract class UserFormVisitor
	{

		// ----------------------------------------------------------------------
		protected UserFormVisitor( Control startControl )
		{
			if ( startControl == null )
			{
				throw new ArgumentNullException( "startControl" );
			}

			this.startControl = startControl;
		} // UserFormVisitor

		// ----------------------------------------------------------------------
		public Control StartControl
		{
			get { return startControl; }
		} // StartControl

		// ----------------------------------------------------------------------
		protected void Start()
		{
			VisitForm( startControl, null );
		} // Start

		// ----------------------------------------------------------------------
		protected virtual void EnterForm( Control control )
		{
		} // EnterForm

		// ----------------------------------------------------------------------
		protected virtual void LeaveForm( Control control )
		{
		} // LeaveForm

		// ----------------------------------------------------------------------
		protected virtual void VisitControl( Control control )
		{
		} // VisitControl

		// ----------------------------------------------------------------------
		protected virtual void VisitHeader( Control control, 
			IUserFormHeader formHeader, IUserFormHeader parentFormHeader )
		{
			if ( parentFormHeader == null )
			{
				VisitMainHeader( control, formHeader );
			}
			else
			{
				VisitSubHeader( control, formHeader, parentFormHeader );
			}
		} // VisitFormHeader

		// ----------------------------------------------------------------------
		protected virtual void VisitMainHeader( Control control, 
			IUserFormHeader formHeader )
		{
		} // VisitMainHeader

		// ----------------------------------------------------------------------
		protected virtual void VisitSubHeader( Control control, 
			IUserFormHeader formHeader, IUserFormHeader parentFormHeader )
		{
		} // VisitSubHeader

		// ----------------------------------------------------------------------
		protected virtual void VisitFormField( Control control,
			IUserFormField formField, IUserFormHeader formHeader )
		{
			IListField listFormField = formField as IListField;
			if ( listFormField != null )
			{
				VisitListFormField( control, listFormField, formHeader );
			}

			ILookupField lookupField = formField as ILookupField;
			if ( lookupField != null )
			{
				VisitLookupFormField( control, lookupField, formHeader );
			}

			IExpressionField expressionField = formField as IExpressionField;
			if ( expressionField != null )
			{
				VisitExpresionFormField( control, expressionField, formHeader );
			}
		} // VisitFormField

		// ----------------------------------------------------------------------
		protected virtual void VisitListFormField( Control control,
			IListField listFormField, IUserFormHeader formHeader )
		{
		} // VisitListFormField

		// ----------------------------------------------------------------------
		protected virtual void VisitLookupFormField( Control control,
			ILookupField lookupField, IUserFormHeader formHeader )
		{
		} // VisitLookupFormField

		// ----------------------------------------------------------------------
		protected virtual void VisitExpresionFormField( Control control,
			IExpressionField expressionField, IUserFormHeader formHeader )
		{
		} // VisitExpresionFormField

		// ----------------------------------------------------------------------
		protected virtual void VisitFormCommand( Control control,
			IUserFormCommand formCommand, IUserFormHeader formHeader )
		{
		} // VisitFormCommand

		// ----------------------------------------------------------------------
		private void VisitForm( Control control, IUserFormHeader parentFormHeader )
		{
			VisitControl( control );
			if ( control.Controls.Count == 0 )
			{
				return;
			}

			IUserFormHeader formHeader = FindFormHeader( control.Controls );
			bool enterForm = false;
			if ( formHeader != null )
			{
				enterForm = true;
				EnterForm( control );
				VisitHeader( control, formHeader, parentFormHeader );
			}
			else
			{
				formHeader = parentFormHeader;
			}

			VisitFormControls( control.Controls, formHeader );

			int controlCount = control.Controls.Count;
			for ( int i = 0; i < controlCount; i++ )
			{
				Control subControl = control.Controls[ i ];
				VisitForm( subControl, formHeader );
				if ( control.Controls.Count != controlCount )
				{
					throw new InvalidOperationException();
				}
			}

			if ( enterForm )
			{
				LeaveForm( control );
			}
		} // VisitForm

		// ----------------------------------------------------------------------
		private static IUserFormHeader FindFormHeader( ControlCollection controls )
		{
			foreach ( Control control in controls )
			{
				IUserFormHeader formHeader = control as IUserFormHeader;
				if ( formHeader != null )
				{
					return formHeader;
				}
			}
			return null;
		} // FindFormHeader

		// ----------------------------------------------------------------------
		private void VisitFormControls( ControlCollection controls, IUserFormHeader formHeader )
		{
			int controlCount = controls.Count;
			for ( int i = 0; i < controlCount; i++ )
			{
				Control control = controls[ i ];

				IUserFormField formField = control as IUserFormField;
				if ( formField != null )
				{
					VisitFormField( control, formField, formHeader );
					if ( controls.Count != controlCount )
					{
						throw new InvalidOperationException();
					}
				}

				IUserFormCommand formCommand = control as IUserFormCommand;
				if ( formCommand != null )
				{
					VisitFormCommand( control, formCommand, formHeader );
				}
			}
		} // VisitFormControls

		// ----------------------------------------------------------------------
		// members
		private readonly Control startControl;

	} // class UserFormVisitor

} // namespace Itenso.WebUserForms.Controls
// -- EOF -------------------------------------------------------------------

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Switzerland Switzerland
👨 Senior .NET Software Engineer

🚀 My Open Source Projects
- Time Period Library 👉 GitHub
- Payroll Engine 👉 GitHub

Feedback and contributions are welcome.



Comments and Discussions