Click here to Skip to main content
15,886,518 members
Articles / Programming Languages / C#

Zeta .NET Base Libraries

Rate me:
Please Sign up or sign in to vote.
4.41/5 (44 votes)
30 Mar 2007CPOL3 min read 151.6K   2.4K   147  
A small set of general-purpose classes for using in .NET applications
namespace ZetaLib.Web.Base
{
	#region Using directives.
	// ----------------------------------------------------------------------

	using System;
	using System.Diagnostics;
	using System.Web.UI;
	using ZetaLib.Web.Common;
	using ZetaLib.Core.Logging;
	using ZetaLib.Web.Properties;
	using System.Web.UI.HtmlControls;
	using ZetaLib.Web.Controls;

	// ----------------------------------------------------------------------
	#endregion

	/////////////////////////////////////////////////////////////////////////

	/// <summary>
	/// Base class for user controls.
	/// </summary>
	public class UserControlBase :
		UserControl
	{
		#region Testing whether JavaScript is enabled.
		// ------------------------------------------------------------------

		/// <summary>
		/// Set to true to enable checking whether JavaScript is enabled.
		/// Does a postback if turned on.
		/// </summary>
		/// <value>
		/// 	<c>true</c> if [want test java script enabled]; otherwise, <c>false</c>.
		/// </value>
		protected bool WantTestJavaScriptEnabled
		{
			get
			{
				return wantTestJavaScriptEnabled;
			}

			set
			{
				wantTestJavaScriptEnabled = value;

				if ( value )
				{
					javaScriptTester = new JavascriptTest();
					this.Controls.Add( javaScriptTester );
				}
				else
				{
					if ( javaScriptTester != null )
					{
						this.Controls.Remove( javaScriptTester );
						javaScriptTester = null;
					}
				}
			}
		}

		private bool wantTestJavaScriptEnabled = false;

		/// <summary>
		/// Checks whether JavaScript is enabled on the client.
		/// </summary>
		/// <value>
		/// 	<c>true</c> if this instance is java script enabled; otherwise, <c>false</c>.
		/// </value>
		protected bool IsJavaScriptEnabled
		{
			get
			{
				object o = Session[@"IsJavaScriptEnabled"];

				if ( o == null )
				{
					if ( !wantTestJavaScriptEnabled )
					{
						throw new ArgumentException(
							string.Format(
							Resources.Str_ZetaLib_Web_Base_UserControlBase_01 ) );
					}
					else
					{
						bool b = javaScriptTester.Enabled;

						if ( IsPostBack )
						{
							Session[@"IsJavaScriptEnabled"] = b;
						}
						return b;
					}
				}
				else
				{
					bool b = Convert.ToBoolean( o );
					return b;
				}
			}
		}

		private JavascriptTest javaScriptTester = null;

		// ------------------------------------------------------------------
		#endregion

		#region Overrides.
		// ------------------------------------------------------------------

		/// <summary>
		/// Raises the <see cref="E:System.Web.UI.Control.Load"></see> event.
		/// </summary>
		/// <param name="e">The <see cref="T:System.EventArgs"></see> object that contains the event data.</param>
		protected override void OnLoad(
			EventArgs e )
		{
			base.OnLoad( e );

			if ( (Page is PageBase) &&
				((PageBase)Page).AutoDataBind )
			{
				LogCentral.Current.LogDebug(
					@"Databinding in ZetaLib.Web.Base.UserControlBase.OnLoad(), " +
					@"because ZetaLib.Web.Base.PageBase.AutoDataBind is true." );

				DataBind();
			}
		}

		/// <summary>
		/// Handles the pressing of the [enter] key on a textbox,
		/// to automatically submit the form.
		/// </summary>
		/// <param name="button">The button.</param>
		/// <returns></returns>
		/// <remarks>
		/// To use this function, simply add the following attribute to
		/// a textbox inside the .ASPX file:
		/// OnKeyDown='GlobalKeyDownEnterHandler(CmdSubmit)'
		/// Where "CmdSubmit" is the ID of the button to press upon [enter].
		/// </remarks>
		protected string GlobalKeyDownEnterHandler(
			Control button )
		{
			return string.Format(
				@"GlobalKeyDownEnterHandler( {0} )",
				button.ClientID );
		}

		// ------------------------------------------------------------------
		#endregion

		#region Public properties.
		// ------------------------------------------------------------------

		/// <summary>
		/// Provides access to an enhanced version of the query string parameters.
		/// </summary>
		/// <value>The QS.</value>
		public QueryString QS
		{
			get
			{
				// I must not create a separate instance but must
				// use the same QS object as the page.
				if ( Page != null && Page is PageBase )
				{
					return ((PageBase)Page).QS;
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Access an instance of the web cache.
		/// </summary>
		/// <value>The page cache.</value>
		public System.Web.Caching.Cache PageCache
		{
			get
			{
				if ( Page != null && Page is PageBase )
				{
					return ((PageBase)Page).PageCache;
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Provides access to the resource manager, that enables to load
		/// multilingual strings from the database.
		/// </summary>
		/// <value>The res man.</value>
		public DBStringResourceManagerBase ResMan
		{
			get
			{
				Debug.Assert(
					Page != null && Page is PageBase,
					@"Page is either NULL or not derived from PageBase." );
				return ((PageBase)Page).ResMan;
			}
		}

		/// <summary>
		/// Get/set the text that appears as an error text on the page.
		/// </summary>
		/// <value>The error text.</value>
		public virtual string ErrorText
		{
			get
			{
				return ErrorTextControl.InnerHtml;
			}
			set
			{
				string s = value;

				ErrorTextControl.InnerHtml = s;
				ErrorTextControl.Visible =
					ErrorTextControl.InnerHtml.Length > 0;
			}
		}

		/// <summary>
		/// Tries to locate a control with the ID 'ErrorTextControl' on
		/// the current page.
		/// </summary>
		/// <value>The error text control.</value>
		public virtual HtmlGenericControl ErrorTextControl
		{
			get
			{
				return (Page as PageBase).ErrorTextControl;
			}
		}

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


Written By
Chief Technology Officer Zeta Software GmbH
Germany Germany
Uwe does programming since 1989 with experiences in Assembler, C++, MFC and lots of web- and database stuff and now uses ASP.NET and C# extensively, too. He has also teached programming to students at the local university.

➡️ Give me a tip 🙂

In his free time, he does climbing, running and mountain biking. In 2012 he became a father of a cute boy and in 2014 of an awesome girl.

Some cool, free software from us:

Windows 10 Ereignisanzeige  
German Developer Community  
Free Test Management Software - Intuitive, competitive, Test Plans.  
Homepage erstellen - Intuitive, very easy to use.  
Offline-Homepage-Baukasten

Comments and Discussions