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

Amazon-esque Pager

Rate me:
Please Sign up or sign in to vote.
4.86/5 (19 votes)
29 Dec 2007BSD6 min read 86K   350   59  
Yet another list pager, but this one can use LinkButtons or simple Hyperlinks, provides scrolling within an ASP.NET AJAX UpdatePanel, and behaves similarly to the paging found on Amazon.com.
/*	<File>
		<Author Name="Daniel Vaughan" Email="dbvaughan NOSPAM at NOSPAM gmail NOSPAM dot NOSPAM com"/>
		<CreationDate>2007/12/28 8:21</CreationDate>
		<LastSubmissionDate>$Date: $</LastSubmissionDate>
		<Version>$Revision: $</Version>
		<License>
Copyright � 2007, Daniel Vaughan
All rights reserved.
http://www.orpius.com

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

- Neither Daniel Vaughan, nor the names of its
contributors may be used to endorse or promote products
derived from this software without specific prior written 
permission. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
		</License>
	</File>
*/

using System;
using System.Web.UI.WebControls;

namespace Orpius.Web.Controls
{
	/// <summary>
	/// Provides paging support for a data controls
	/// such as the <see cref="GridView"/>.
	/// </summary>
	public class DataControlAdapter
	{
		GridView gridView;

		int pageIndex;
		int itemCount;
		int itemsPerPage;

		PagerControl pagerControl;

		public PagerControl PagerControl
		{
			get
			{
				return pagerControl;
			}
			set
			{
				if (pagerControl != null)
				{
					pagerControl.PageChanging -= new PageChangeEventHandler(pagerControl_PageChanging);
				}
				pagerControl = value;
				if (pagerControl != null)
				{
					pagerControl.PageChanging += new PageChangeEventHandler(pagerControl_PageChanging);
				}
			}
		}

		void pagerControl_PageChanging(object sender, PageChangeEventArgs e)
		{
			PageIndex = e.PageIndex;

			if (gridView != null)
			{
				gridView.PageIndex = e.PageIndex;
				gridView.DataBind();
			}
		}

		/// <summary>
		/// Gets or sets the index of the current page that is showing.
		/// </summary>
		/// <value>The index of the current page.</value>
		int PageIndex
		{
			get
			{
				return pageIndex;
			}
			set
			{
				pageIndex = value;
				if (gridView != null)
				{
					gridView.PageIndex = value;
				}
			}
		}

		/// <summary>
		/// Gets or sets the total number of items in the paging list.
		/// </summary>
		/// <value>The item count.</value>
		public int ItemCount
		{
			get
			{
				return itemCount;
			}
			set
			{
				itemCount = value > 0 ? value : 0;
			}
		}

		/// <summary>
		/// Gets or sets the maximum number of items displayed on each page.
		/// </summary>
		/// <value>The items per page.</value>
		int ItemsPerPage
		{
			get
			{
				return itemsPerPage;
			}
			set
			{
				itemsPerPage = value;
			}
		}

		public DataControlAdapter(GridView gridView, int itemCount)
		{
			if (gridView == null)
			{
				throw new ArgumentNullException("gridView");
			}
			this.gridView = gridView;
			gridView.PreRender += new EventHandler(gridView_Load);
			gridView.RowDeleting += new GridViewDeleteEventHandler(gridView_RowDeleting);
			ItemCount = itemCount;
		}

		void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
		{
			if (!e.Cancel)
			{
				ItemCount--;
			}
		}

		void gridView_Load(object sender, EventArgs e)
		{
			pagerControl.ItemsPerPage = ItemsPerPage = gridView.PageSize;
			pagerControl.ItemCount = ItemCount;
			pagerControl.PageIndex = PageIndex = gridView.PageIndex;
		}
	}
}

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 BSD License


Written By
Engineer
Switzerland Switzerland
Daniel is a former senior engineer in Technology and Research at the Office of the CTO at Microsoft, working on next generation systems.

Previously Daniel was a nine-time Microsoft MVP and co-founder of Outcoder, a Swiss software and consulting company.

Daniel is the author of Windows Phone 8 Unleashed and Windows Phone 7.5 Unleashed, both published by SAMS.

Daniel is the developer behind several acclaimed mobile apps including Surfy Browser for Android and Windows Phone. Daniel is the creator of a number of popular open-source projects, most notably Codon.

Would you like Daniel to bring value to your organisation? Please contact

Blog | Twitter


Xamarin Experts
Windows 10 Experts

Comments and Discussions