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

Zeta Enterprise Library

Rate me:
Please Sign up or sign in to vote.
4.97/5 (14 votes)
16 Jan 2010CPOL3 min read 50.3K   2.3K   48  
A small set of general-purpose classes for using in .NET applications (2.0 or higher)
namespace Zeta.EnterpriseLibrary.Web.Controls
{
	#region Using directives.
	// ----------------------------------------------------------------------

	using System;
	using System.Web.UI;
	using System.Web.UI.WebControls;
	using Common;
	using Properties;

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

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

	/// <summary>
	/// Class that manages paging of items, in a Google-fashion; i.e. not all
	/// pages are listed but only the pages surrounding the current active page.
	/// </summary>
	public class PostBackGooglePager :
		Control,
		INamingContainer
	{
		#region Events.
		// ------------------------------------------------------------------

		/// <summary>
		/// Raised when a page is about to be changing.
		/// </summary>
		public event PostBackGooglePagerPageChangeEventHandler PageChanging;

		/// <summary>
		/// Raised when a page was changed.
		/// </summary>
		public event PostBackGooglePagerPageChangeEventHandler PageChanged;

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

		#region Constructors.
		// ------------------------------------------------------------------

		/// <summary>
		/// Default constructor.
		/// </summary>
		public PostBackGooglePager()
		{
			Prefix = string.Empty;
			InitIt();
		}

		/// <summary>
		/// In order to use multiple pagers on one page, you can specify
		/// a prefix to use for this instance.
		/// </summary>
		/// <param name="prefix">The prefix to use</param>
		public PostBackGooglePager(
			string prefix )
		{
			Prefix = prefix;
			InitIt();
		}

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

		#region Public methods.
		// ------------------------------------------------------------------

		/// <summary>
		/// Reset variables.
		/// </summary>
		public void InitIt()
		{
			Counter = 0;
			Anchor = string.Empty;

			// calculate.
			CalcValues();
		}

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

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

		/// <summary>
		/// The index of the first visible item.
		/// </summary>
		/// <value>The index of the lower item.</value>
		public int LowerItemIndex
		{
			get
			{
				return ItemOffset;
			}
		}

		/// <summary>
		/// The index of the last visible item.
		/// </summary>
		/// <value>The index of the upper item.</value>
		public int UpperItemIndex
		{
			get
			{
				return Math.Min(
					ItemOffset + ItemsPerPage - 1,
					TotalItemCount - 1 );
			}
		}

		/// <summary>
		/// Gets or sets the item offset.
		/// </summary>
		/// <value>The item offset.</value>
		public int ItemOffset
		{
			get
			{
				if ( ViewState[@"Offset"] == null )
				{
					return 0;
				}
				else
				{
					return (int)ViewState[@"Offset"];
				}
			}
			set
			{
				ViewState[@"Offset"] = value;
				CalcValues();
			}
		}

		/// <summary>
		/// The total number of items in the pager.
		/// </summary>
		/// <value>The total item count.</value>
		public int TotalItemCount
		{
			get
			{
				if ( ViewState[@"Size"] == null )
				{
					return 0;
				}
				else
				{
					return (int)ViewState[@"Size"];
				}
			}
			set
			{
				ViewState[@"Size"] = value;
				CalcValues();
			}
		}

		/// <summary>
		/// Gets or sets the prefix.
		/// </summary>
		/// <value>The prefix.</value>
		public string Prefix
		{
			get
			{
				return ConvertHelper.ToString( ViewState[@"Prefix"] );
			}
			set
			{
				ViewState[@"Prefix"] = value;
			}
		}

		/// <summary>
		/// Gets or sets the counter.
		/// </summary>
		/// <value>The counter.</value>
		public int Counter
		{
			get
			{
				return ConvertHelper.ToInt32( ViewState[@"Counter"] );
			}
			set
			{
				ViewState[@"Counter"] = value;
			}
		}

		/// <summary>
		/// Gets or sets the anchor.
		/// </summary>
		/// <value>The anchor.</value>
		public string Anchor
		{
			get
			{
				return ConvertHelper.ToString( ViewState[@"Anchor"] );
			}
			set
			{
				ViewState[@"Anchor"] = value;
			}
		}

		/// <summary>
		/// Gets or sets the pages count.
		/// </summary>
		/// <value>The pages count.</value>
		public int PagesCount
		{
			get
			{
				return ConvertHelper.ToInt32( ViewState[@"PagesCount"] );
			}
			set
			{
				ViewState[@"PagesCount"] = value;
			}
		}

		/// <summary>
		/// Gets or sets the real pages count.
		/// </summary>
		/// <value>The real pages count.</value>
		public int RealPagesCount
		{
			get
			{
				return ConvertHelper.ToInt32( ViewState[@"RealPagesCount"] );
			}
			set
			{
				ViewState[@"RealPagesCount"] = value;
			}
		}

		/// <summary>
		/// Gets or sets the current page number.
		/// </summary>
		/// <value>The current page number.</value>
		public int CurrentPageNumber
		{
			get
			{
				return ConvertHelper.ToInt32( ViewState[@"CurrentPageNumber"] );
			}
			set
			{
				ViewState[@"CurrentPageNumber"] = value;
			}
		}

		/// <summary>
		/// Gets or sets a value indicating whether [more pages available].
		/// </summary>
		/// <value><c>true</c> if [more pages available]; otherwise, <c>false</c>.</value>
		public bool MorePagesAvailable
		{
			get
			{
				return ConvertHelper.ToBoolean(
					ViewState[@"MorePagesAvailable"] );
			}
			set
			{
				ViewState[@"MorePagesAvailable"] = value;
			}
		}

		/// <summary>
		/// Gets or sets the pager corona.
		/// </summary>
		/// <value>The pager corona.</value>
		public int PagerCorona
		{
			get
			{
				return ConvertHelper.ToInt32(
					ViewState[@"PagerCorona"],
					DefaultPagerCorona );
			}
			set
			{
				ViewState[@"PagerCorona"] = value;
			}
		}

		/// <summary>
		/// Gets or sets the items per page.
		/// </summary>
		/// <value>The items per page.</value>
		public int ItemsPerPage
		{
			get
			{
				return ConvertHelper.ToInt32(
					ViewState[@"ItemsPerPage"],
					DefaultItemsPerPage );
			}
			set
			{
				ViewState[@"ItemsPerPage"] = value;
			}
		}

		/// <summary>
		/// Gets or sets the max pages count.
		/// </summary>
		/// <value>The max pages count.</value>
		public int MaxPagesCount
		{
			get
			{
				return ConvertHelper.ToInt32(
					ViewState[@"MaxPagesCount"],
					DefaultMaxPagesCount );
			}
			set
			{
				ViewState[@"MaxPagesCount"] = value;
			}
		}

		/// <summary>
		/// Configuration values.
		/// </summary>
		public static readonly int DefaultPagerCorona = 8;
		public static readonly int DefaultItemsPerPage = 20;
		public static readonly int DefaultMaxPagesCount = 20;

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

		#region Private methods.
		// ------------------------------------------------------------------

		/// <summary>
		/// Calcs the values.
		/// </summary>
		protected void CalcValues()
		{
			PagesCount = TotalItemCount / ItemsPerPage;
			if ( TotalItemCount % ItemsPerPage > 0 )
			{
				PagesCount++;
			}

			RealPagesCount = PagesCount;

			CurrentPageNumber = ItemOffset / ItemsPerPage;
		}

		/// <summary>
		/// Raises the event.
		/// </summary>
		/// <param name="e">The PostBackGooglePagerPageChangeEventArgs
		/// instance containing the event data.</param>
		protected void OnPageChanging(
			PostBackGooglePagerPageChangeEventArgs e )
		{
			if ( PageChanging != null )
			{
				PageChanging( this, e );
			}
		}

		/// <summary>
		/// Raises the event.
		/// </summary>
		/// <param name="e">The PostBackGooglePagerPageChangeEventArgs
		/// instance containing the event data.</param>
		protected void OnPageChanged(
			PostBackGooglePagerPageChangeEventArgs e )
		{
			if ( PageChanged != null )
			{
				PageChanged( this, e );
			}
		}

		/// <summary>
		/// Called by the ASP.NET page framework to notify server controls 
		/// that use composition-based implementation to create any child
		/// controls they contain in preparation for posting back or rendering.
		/// </summary>
		protected override void CreateChildControls()
		{
			Controls.Clear();

			// again here.
			CalcValues();

			System.Diagnostics.Trace.WriteLine(
				string.Format(
				@"Pager size: {0}.",
				TotalItemCount ) );

			if ( TotalItemCount > ItemsPerPage )
			{
				// --
				// The left links.

				if ( ItemOffset > 0 )
				{
					CreateHtmlLeftmostPage();
					CreateHtmlOneLeftPage();
					CreateHtmlSeparator();
				}

				// --
				// The inner links.

				int navigateLeftPage =
					Math.Max( 0,
					CurrentPageNumber - PagerCorona );
				int navigateRightPage =
					Math.Min( RealPagesCount - 1,
					CurrentPageNumber + PagerCorona - 1 );

				for ( int i = navigateLeftPage; i <= navigateRightPage; ++i )
				{
					if ( i > navigateLeftPage )
					{
						CreateHtmlSeparator();
					}

					if ( i == CurrentPageNumber )
					{
						CreateHtmlActivePage( i );
					}
					else
					{
						CreateHtmlInactivePage( i );
					}
				}

				// --
				// The right links.

				if ( CurrentPageNumber < RealPagesCount - 1 )
				{
					CreateHtmlSeparator();
					CreateHtmlOneRightPage();
					CreateHtmlRightmostPage();
				}
			}
		}

		/// <summary>
		/// Creates the HTML inactive page.
		/// </summary>
		/// <param name="pageNumber">The page number, zero-based.</param>
		protected void CreateHtmlInactivePage(
			int pageNumber )
		{
			LinkButton a = new LinkButton();
			a.Click += GotoDiscretePage_Click;
			a.Text = (pageNumber + 1).ToString();
			// Put additional information into an attribute.
			a.Attributes[@"PageNo"] = pageNumber.ToString();
			a.ToolTip =
				Resources.Str_Controls_GooglePager_ShowPageNumber.
				Replace( @"PageNumber", (pageNumber + 1).ToString() );

			Controls.Add( a );
		}

		/// <summary>
		/// Creates the HTML active page.
		/// </summary>
		/// <param name="pageNumber">The page number.</param>
		protected void CreateHtmlActivePage(
			int pageNumber )
		{
			Controls.Add( new LiteralControl(
				string.Format( @" <b>{0}</b> ", (pageNumber + 1) ) ) );
		}

		/// <summary>
		/// Creates the HTML separator.
		/// </summary>
		protected void CreateHtmlSeparator()
		{
			Controls.Add( new LiteralControl(
				@" <span style='color: silver'>|</span> " ) );
		}

		/// <summary>
		/// Creates the HTML leftmost page.
		/// </summary>
		protected void CreateHtmlLeftmostPage()
		{
			LinkButton a = new LinkButton();
			a.Click += GotoLeftmost_Click;
			a.Text = @"<<";
			a.ToolTip = Resources.Str_Controls_GooglePager_ShowFirstPage;

			Controls.Add( new LiteralControl( @" " ) );
			Controls.Add( a );
			Controls.Add( new LiteralControl( @" " ) );
		}

		/// <summary>
		/// Creates the HTML one left page.
		/// </summary>
		protected void CreateHtmlOneLeftPage()
		{
			LinkButton a = new LinkButton();
			a.Click += GotoOneLeft_Click;
			a.Text = @"<";
			a.ToolTip = Resources.Str_Controls_GooglePager_GoOnePageLeft;

			Controls.Add( new LiteralControl( @" " ) );
			Controls.Add( a );
			Controls.Add( new LiteralControl( @" " ) );
		}

		/// <summary>
		/// Creates the HTML rightmost page.
		/// </summary>
		protected void CreateHtmlRightmostPage()
		{
			LinkButton a = new LinkButton();
			a.Click += GotoRightmost_Click;
			a.Text = @">>";
			a.ToolTip = Resources.Str_Controls_GooglePager_ShowLastPage;

			Controls.Add( new LiteralControl( @" " ) );
			Controls.Add( a );
			Controls.Add( new LiteralControl( @" " ) );
		}

		/// <summary>
		/// Creates the HTML one right page.
		/// </summary>
		protected void CreateHtmlOneRightPage()
		{
			LinkButton a = new LinkButton();
			a.Click += GotoOneRight_Click;
			a.Text = @">";
			a.ToolTip = Resources.Str_Controls_GooglePager_GoOnePageRight;

			Controls.Add( new LiteralControl( @" " ) );
			Controls.Add( a );
			Controls.Add( new LiteralControl( @" " ) );
		}

		/// <summary>
		/// Enforces the (re-)creation of child controls.
		/// </summary>
		protected void RecreateChildControls()
		{
			ChildControlsCreated = false;

			CreateChildControls();
		}

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

		#region Handler.
		// ------------------------------------------------------------------

		/// <summary>
		/// Event handler.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		protected void GotoDiscretePage_Click(
			object sender,
			EventArgs e )
		{
			// read the additional information back from an attribute.
			int pageNumber = Convert.ToInt32(
				((LinkButton)sender).Attributes[@"PageNo"] );

			// Notify event-sinks.
			PostBackGooglePagerPageChangeEventArgs f =
				new PostBackGooglePagerPageChangeEventArgs(
				CurrentPageNumber,
				pageNumber );
			OnPageChanging( f );

			// Set new.
			ItemOffset = pageNumber * ItemsPerPage;
			RecreateChildControls();

			// Notify after.
			OnPageChanged( f );
		}

		/// <summary>
		/// Event handler.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		protected void GotoLeftmost_Click(
			object sender,
			EventArgs e )
		{
			// Notify event-sinks.
			PostBackGooglePagerPageChangeEventArgs f =
				new PostBackGooglePagerPageChangeEventArgs(
				CurrentPageNumber,
				0 );
			OnPageChanging( f );

			// Set new.
			ItemOffset = 0;
			RecreateChildControls();

			// Notify after.
			OnPageChanged( f );
		}

		/// <summary>
		/// Event handler.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		protected void GotoOneLeft_Click(
			object sender,
			EventArgs e )
		{
			// Notify event-sinks.
			PostBackGooglePagerPageChangeEventArgs f =
				new PostBackGooglePagerPageChangeEventArgs(
				CurrentPageNumber,
				CurrentPageNumber - 1 );
			OnPageChanging( f );

			// Set new.
			ItemOffset -= ItemsPerPage;
			RecreateChildControls();

			// Notify after.
			OnPageChanged( f );
		}

		/// <summary>
		/// Event handler.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		protected void GotoRightmost_Click(
			object sender,
			EventArgs e )
		{
			// Notify event-sinks.
			PostBackGooglePagerPageChangeEventArgs f =
				new PostBackGooglePagerPageChangeEventArgs(
				CurrentPageNumber,
				PagesCount - 1 );
			OnPageChanging( f );

			// Set new.
			ItemOffset = (PagesCount - 1) * ItemsPerPage;
			RecreateChildControls();

			// Notify after.
			OnPageChanged( f );
		}

		/// <summary>
		/// Event handler.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		protected void GotoOneRight_Click(
			object sender,
			EventArgs e )
		{
			// Notify event-sinks.
			PostBackGooglePagerPageChangeEventArgs f =
				new PostBackGooglePagerPageChangeEventArgs(
				CurrentPageNumber,
				CurrentPageNumber + 1 );
			OnPageChanging( f );

			// Set new.
			ItemOffset += ItemsPerPage;
			RecreateChildControls();

			// Notify after.
			OnPageChanged( f );
		}

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