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

ASP.NET Adapter Control

Rate me:
Please Sign up or sign in to vote.
4.80/5 (2 votes)
29 Mar 2012CPOL1 min read 12.1K   2  
Adapt properties like visibility or enabling between ASP.NET Controls.
// -- FILE ------------------------------------------------------------------
// name       : AdapterControl.cs
// project    : Itenso Script Panel
// created    : Jani Giannoudis - 2012.03.29
// language   : c#
// environment: .NET 4.0
// copyright  : (c) 2012 by Itenso GmbH, Switzerland
// --------------------------------------------------------------------------
using System;
using System.Diagnostics;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Itenso.Community.AdapterControl.Controls
{

	// ------------------------------------------------------------------------
	public class AdapterControl : Control
	{

		// ----------------------------------------------------------------------
		public enum AdaptMode
		{
			None,
			Unvaried,
			Inverted,
		} // enum AdaptMode

		// ----------------------------------------------------------------------
		[Themeable( false )]
		public string SourceControl { get; set; }

		// ----------------------------------------------------------------------
		[Themeable( false )]
		public string TargetControl { get; set; }

		// ----------------------------------------------------------------------
		[Themeable( false )]
		public AdaptMode EnabledMode { get; set; }

		// ----------------------------------------------------------------------
		[Themeable( false )]
		public AdaptMode VisibleMode { get; set; }

		// ----------------------------------------------------------------------
		protected override void OnPreRender( EventArgs e )
		{
			base.OnPreRender( e );

			// source
			if ( string.IsNullOrEmpty( SourceControl ) )
			{
				Trace.WriteLine( "AdapterControl: missing source control" );
				return;
			}
			Control source = Parent.FindControl( SourceControl );
			if ( source == null )
			{
				Trace.WriteLine( "AdapterControl: source control '" + SourceControl + "' not found" );
				return;
			}

			// target
			if ( string.IsNullOrEmpty( TargetControl ) )
			{
				Trace.WriteLine( "AdapterControl: missing target control" );
				return;
			}
			Control target = Parent.FindControl( TargetControl );
			if ( target == null )
			{
				Trace.WriteLine( "AdapterControl: target control '" + TargetControl + "' not found" );
				return;
			}

			switch ( VisibleMode )
			{
				case AdaptMode.Unvaried:
					target.Visible = source.Visible;
					break;
				case AdaptMode.Inverted:
					target.Visible = !source.Visible;
					break;
			}

			if ( target.Visible && EnabledMode != AdaptMode.None )
			{
				WebControl sourceWebControl = source as WebControl;
				WebControl targetWebControl = target as WebControl;
				if ( sourceWebControl != null && targetWebControl != null )
				{
					switch ( EnabledMode )
					{
						case AdaptMode.Unvaried:
							targetWebControl.Enabled = sourceWebControl.Enabled;
							break;
						case AdaptMode.Inverted:
							targetWebControl.Enabled = !sourceWebControl.Enabled;
							break;
					}
				}
			}
		} // OnPreRender

	} // class AdapterControl

} // namespace Itenso.Community.AdapterControl.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