Click here to Skip to main content
15,892,005 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.5K   4.5K   187  
User driven runtime dynamic ASP.NET Web Forms
// -- FILE ------------------------------------------------------------------
// name       : UserFormFieldDesigner.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.IO;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.Design;
using System.Drawing;

namespace Itenso.WebUserForms.Controls
{

	// ------------------------------------------------------------------------
	public class UserFormFieldDesigner : ControlDesigner
	{

		// ----------------------------------------------------------------------
		public UserFormFieldDesigner()
		{
		} // UserFormFieldDesigner

		// ----------------------------------------------------------------------
		protected Control DesignControl
		{
			get { return Component as Control; }
		} // DesignControl

		// ----------------------------------------------------------------------
		protected Color DefaultFieldNameColor
		{
			get { return this.defaultFieldNameColor; }
			set { this.defaultFieldNameColor = value; }
		} // DefaultFieldNameColor

		// ----------------------------------------------------------------------
		protected Color MissingFieldNameColor
		{
			get { return this.missingFieldNameColor; }
			set { this.missingFieldNameColor = value; }
		} // MissingFieldNameColor

		// ----------------------------------------------------------------------
		protected Color InvalidFieldNameColor
		{
			get { return this.invalidFieldNameColor; }
			set { this.invalidFieldNameColor = value; }
		} // InvalidFieldNameColor

		// ----------------------------------------------------------------------
		protected string FieldName
		{
			get
			{
				IUserFormField webformField = Component as IUserFormField;
				if ( webformField == null )
				{
					throw new InvalidOperationException( "IUserFormField required" );
				}
				return webformField.FieldName;
			}
		} // FieldName

		// ----------------------------------------------------------------------
		protected string FieldValue
		{
			get
			{
				IUserFormField webformField = Component as IUserFormField;
				if ( webformField == null )
				{
					throw new InvalidOperationException( "IUserFormField required" );
				}
				return webformField.FieldValue;
			}
		} // FieldValue

		// ----------------------------------------------------------------------
		protected bool HasFieldName
		{
			get { return !string.IsNullOrEmpty( FieldName ); }
		} // HasFieldName

		// ----------------------------------------------------------------------
		protected bool HasFieldValue
		{
			get { return !string.IsNullOrEmpty( FieldValue ); }
		} // HasFieldValue

		// ----------------------------------------------------------------------
		protected Control ConflictingControl
		{
			get
			{
				Control designControl = DesignControl;
				if ( designControl == null )
				{
					return null;
				}

				Page page = designControl.Page;
				if ( page == null )
				{
					return null;
				}

				return FindConflictingControl( page.Controls, FieldName );
			}
		} // ConflictingControl

		// ----------------------------------------------------------------------
		protected bool HasConfilictingControl
		{
			get { return ConflictingControl != null; }
		} // HasConfilictingControl

		// ----------------------------------------------------------------------
		public override string GetDesignTimeHtml()
		{
			Control designControl = DesignControl;
			if ( designControl == null )
			{
				return base.GetDesignTimeHtml();
			}

			WebControl designWebControl = designControl as WebControl;
			if ( designWebControl != null )
			{
				designWebControl.BackColor = GetStatusColor();
			}
			return GetRenderHtml( designControl );
		} // GetDesignTimeHtml

		// ----------------------------------------------------------------------
		protected virtual Color GetStatusColor()
		{
			if ( !HasFieldName )
			{
				return MissingFieldNameColor;
			}

			if ( HasConfilictingControl )
			{
				return InvalidFieldNameColor;
			}

			return DefaultFieldNameColor;
		} // GetStatusColor

		// ----------------------------------------------------------------------
		protected virtual string GetRenderHtml( Control control )
		{
			if ( control is WebControl )
			{
				StringWriter text = new StringWriter();
				HtmlTextWriter writer = new HtmlTextWriter( text );
				control.RenderControl( writer );
				return text.ToString();
			}

			StringBuilder sb = new StringBuilder();
			string fieldName = FieldName;
			if ( !string.IsNullOrEmpty( fieldName ) )
			{
				sb.Append( fieldName );
			}
			string fieldValue = FieldValue;
			if ( !string.IsNullOrEmpty( fieldValue ) )
			{
				if ( sb.Length != 0 )
				{
					sb.Append( " - " );
				}
				sb.Append( fieldValue );
			}

			return CreatePlaceHolderDesignTimeHtml( sb.ToString() );
		} // GetRenderHtml

		// ----------------------------------------------------------------------
		private Control FindConflictingControl( ControlCollection controls, string fieldName )
		{
			if ( controls == null || controls.Count == 0 )
			{
				return null;
			}

			foreach ( Control control in controls )
			{
				IUserFormField formField = control as IUserFormField;
				if ( formField != null )
				{
					if ( fieldName.Equals( formField.FieldName ) && control != DesignControl )
					{
						return control;
					}
				}

				if ( control.Controls.Count > 0 )
				{
					Control subControl = FindConflictingControl( control.Controls, fieldName );
					if ( subControl != null )
					{
						return subControl;
					}
				}
			}

			return null;
		} // FindConflictingControl

		// ----------------------------------------------------------------------
		// members
		private Color defaultFieldNameColor = Color.LightGreen;
		private Color missingFieldNameColor = Color.Red;
		private Color invalidFieldNameColor = Color.DarkSalmon;

	} // class UserFormFieldDesigner

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