Click here to Skip to main content
15,884,743 members
Articles / Web Development / IIS

An Extensible Master-Page Framework for ASP.NET 1.1 Using Pattern Oriented Design

Rate me:
Please Sign up or sign in to vote.
4.90/5 (95 votes)
30 Jun 20049 min read 353K   6.5K   245  
Development of a framework for master-pages using ASP.NET and C#.
#region Copyright(C) M.Shams Mukhtar (shams_mukhtar@yahoo.com)
//
// You are free to use or modify the code, as long as you place
// copyright notice above. Thanks!
//
// Filename: MasterPageBase.cs
#endregion

#region Instructions
#endregion

#region Remarks
#endregion

#region Namespaces used
	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 System.Configuration;
#endregion

namespace Shams.Web.UI.MasterPages
{
	/// <summary>
	/// MasterPageBase, As the name suggest, its a base class for all the MasterPages
	/// </summary>
	public abstract class MasterPageBase : System.Web.UI.Page, IPlaceHolders
	{
		/// <summary>PageControlBase, responsible for All inner HTML stuff</summary>
		protected PageControlBase pageController;
		/// <summary>PageUserControlBase, responsible for All Visual stuff</summary>
		protected PageUserControlBase userPageControl;

		/// <summary>
		/// MasterPageBase Constructor
		/// </summary>
		public MasterPageBase()	
		{ 
			pageController = MasterPageFactory.CreateMasterPageControl();
		}

		override protected void OnInit(EventArgs e)
		{	
			PopulateControls();
			AddPlaceHolderControls();	
			
			base.OnInit(e);

			// if you still want to add something else at this point...
			PostInitialize(e);
		}

		protected void PopulateControls()
		{			
			userPageControl = (PageUserControl) LoadPageUserControl();		
			// Add to the form-control embedded in the PageController
			pageController.AddToHtmlForm(userPageControl);
			// Add page controls to the pageController
			AddControlsToPageController();
			// Now populate page with the controller
			PopulatePageControls();
		}

		protected void AddControlsToPageController()
		{
			if (this.HasControls())
			{
				for (int i=0; i<this.Controls.Count; ++i)
				{
					AddChildControl(this.Controls[i]);
				}
			}
		}
		
		/// <summary>
		/// Override this function if you want Childs of the page added differently
		/// </summary>
		/// <param name="control"></param>
		virtual protected void AddChildControl(System.Web.UI.Control control)
		{
			pageController.AddToHtmlForm(control);
		}
		
		/// <summary>
		/// 
		/// </summary>
		protected void PopulatePageControls()
		{
			// clear the contents of this page.. 
			this.Controls.Clear();

			// add pageController to the this page, containing all the controls.
			this.Controls.AddAt(0, pageController);
		}

		/// <summary>
		/// Override it, if you want to put your-own PageUserControl
		/// </summary>
		virtual protected System.Web.UI.Control LoadPageUserControl()
		{	
			return MasterPageFactory.CreateMasterUserControl(this);
		}

		/// <summary>
		/// Call this to hide the Navigation PlaceHolder
		/// </summary>
		/// <param name="navFlag"></param>
		protected void ToggleNavigation(bool navFlag)
		{
			if (navFlag)
			{
				this.userPageControl.PlaceHolderNavigation.Visible = true;
				this.userPageControl.PlaceHolderSiteCounter.Visible = true;			
			}
			else
			{
				this.userPageControl.PlaceHolderNavigation.Visible = false;
				this.userPageControl.PlaceHolderSiteCounter.Visible = false;			
			}
		}

		protected void ToggleLogoBar(bool navFlag)
		{
			if (navFlag)
			{
				this.userPageControl.PlaceHolderLogo.Visible = true;
			}
			else
			{
				this.userPageControl.PlaceHolderLogo.Visible = false;			
			}
		}

		/// <summary>
		/// Call this function from the derived class to clear the masterPage contents, if any
		/// </summary>
		protected void ClearPlaceHolderControls()
		{
			this.userPageControl.PlaceHolderLogo.Controls.Clear();
			this.userPageControl.PlaceHolderHeader.Controls.Clear();
			this.userPageControl.PlaceHolderMenu.Controls.Clear();
			this.userPageControl.PlaceHolderSubMenu.Controls.Clear();
			this.userPageControl.PlaceHolderNavigation.Controls.Clear();
			this.userPageControl.PlaceHolderContents.Controls.Clear();
			this.userPageControl.PlaceHolderSiteCounter.Controls.Clear();
			this.userPageControl.PlaceHolderFooter.Controls.Clear();
		}

		/// <summary>
		/// Override this function to add controls in place-holders 
		/// </summary>
		virtual public void AddPlaceHolderControls()
		{
			System.Diagnostics.Debug.WriteLine("AddPlaceHolderControls() - Called");

			AddPlaceHolderHeader();
			AddPlaceHolderLogo();
			AddPlaceHolderMenu();
			AddPlaceHolderSubMenu();
			AddPlaceHolderNavigation();
			AddPlaceHolderContents();
			AddPlaceHolderSiteCounter();
			AddPlaceHolderFooter();
		}

		#region IPlaceHolders realizations

		public System.Web.UI.Control ParentControl()
		{
			return this;
		}

		virtual public void AddPlaceHolderHeader()
		{
			System.Diagnostics.Debug.WriteLine("AddPlaceHolderHeader() - Called");
		}
		virtual public void AddPlaceHolderLogo()
		{
			System.Diagnostics.Debug.WriteLine("AddPlaceHolderLogo() - Called");
		}
		virtual public void AddPlaceHolderMenu()
		{
			System.Diagnostics.Debug.WriteLine("AddPlaceHolderMenu() - Called");
		}
		virtual public void AddPlaceHolderSubMenu()
		{
			System.Diagnostics.Debug.WriteLine("AddPlaceHolderSubMenu() - Called");
		}
		virtual public void AddPlaceHolderNavigation()
		{
			System.Diagnostics.Debug.WriteLine("AddPlaceHolderNavigation() - Called");
		}
		virtual public void AddPlaceHolderContents()
		{
			System.Diagnostics.Debug.WriteLine("AddPlaceHolderContents() - Called");
		}
		virtual public void AddPlaceHolderSiteCounter()
		{
			System.Diagnostics.Debug.WriteLine("AddPlaceHolderSiteCounter() - Called");
		}
		virtual public void AddPlaceHolderFooter()
		{
			System.Diagnostics.Debug.WriteLine("AddPlaceHolderFooter() - Called");			
		}
		
		#endregion

		protected void PostInitialize(EventArgs e)
		{
			System.Diagnostics.Debug.WriteLine("PostInitialize() - Called");
		}

		private void AddPageControlsToHtmlForm(HtmlForm htmlForm)
		{
			System.Web.UI.Control currentPageControl = null;
			IEnumerator controlsIterator = this.Controls.GetEnumerator();
			// skip the first one...
			controlsIterator.MoveNext();	
			// iterate through whole list...
			while (controlsIterator.MoveNext())
			{
				currentPageControl = (System.Web.UI.Control)controlsIterator.Current;
				htmlForm.Controls.Add(currentPageControl);
				this.Controls.Remove(currentPageControl);
			}
		}

		protected System.Web.UI.UserControl LoadUserControl(string urlUserControl)
		{	
			System.Web.UI.UserControl userControl = null;
			try
			{
				userControl = (System.Web.UI.UserControl)Page.LoadControl(urlUserControl);
			}
			catch (Exception ex)
			{
				throw ex;
			}		
			return userControl;
		}
	}
}

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
Chief Technology Officer GeeksCafe.NET | Infortran.COM
United States United States
Lead Architect with 20+ years of software design and development experience. Architected and designed many industrial softwares and passed through full software development life-cycle. Strong hold in Object-Oriented software engineering using UML with Design Patterns, C#, .NET, C++/VC++ and Java. Domain expertise are in Distributed Computing along with WCF/Web services, Messaging Systems (MSMQ), Multi-threading, Component developments, Computer Graphics, Embedded Systems, GIS development, framework development, User-Interface designs (WPF .NET), Rule based development (WF .NET), Chemical Engineering and Process Controls. Having both Bachelors and Masters degrees in Engineering with certifications in Obect Oriented Analysis and design. Smile | :)

Email: shams.mukhtar@gmail.com
Blog Link: http://www.geekscafe.net


Comments and Discussions