Home
Products
Forums
Help
Publish Article
Go Freelance

Smart Paging In DataList Control

Downloads

If you are seeing this section and do not see download links, this means that you are not logged into our site. If you already are a member, click on the login link and login into site and come back to this page for downloading the control files. If you are not a member, click on registration link to become a Winista member and download the control for free.

DataList paging

In my previous article How to use paging in DataList control, I described how to do basic implementation of a pager for DataList control. That type of implementation works very well when you have a small data set and does not span lot of pages. In real world that is not always the case and you ends up data that spans dozens and hundreds of pages. In that case you have to come with some mechanism to provide user a way to navigate through the pages and not clutter the pager part of the control.

I came with an implementation that looks like the user interface as shown above. The logic behind this implementation is contained inside BindPager function on that page.

private void BindPager()
{
	if (_totalPages <= 1)
	{
		TopPager_Panel.Visible = BottomPager_Panel.Visible = false;
		return;
	}

	TopPager_Panel.Visible = BottomPager_Panel.Visible = true;

	bool showFirstPageLink;
	bool showLastPageLink;
	bool showStartElipticalLink;
	bool showEndElipticalLink;
	int startIdx = 1;
	int endIdx = _maxPagesInPager;
	if (_totalPages > _maxPagesInPager && _pageIdx > _maxPagesInPager-2)
	{
		endIdx = _pageIdx + 1;
		startIdx = endIdx - _maxPagesInPager + 1;

		if (endIdx > _totalPages)
		{
			endIdx = _totalPages;
		}
		if (startIdx <= 0)
		{
			startIdx = 1;
		}
	}

	showStartElipticalLink = showFirstPageLink = (startIdx > 1);
	showEndElipticalLink = showLastPageLink = (endIdx < _totalPages);

	if (showFirstPageLink)
	{
		if (_showTopPager)
		{
			AddPagerLink(this.TopPager_Panel, 1, "First Page", "First Page", true, false, false);
		}

		if (_showBottomPager)
		{
			AddPagerLink(this.BottomPager_Panel, 1, "First Page", "First Page", true, false, false);
		}
	}

	if (showStartElipticalLink)
	{
		if (_showTopPager)
		{
			AddPagerLink(this.TopPager_Panel, startIdx - 1, "..", "Previous Page", true, true, false);
		}

		if (_showBottomPager)
		{
			AddPagerLink(this.BottomPager_Panel, startIdx - 1, "..", "Previous Page", true, true, false);
		}
	}

	for (int i = startIdx; i <= endIdx; i++)
	{
		HyperLink topLink = new HyperLink();
		HyperLink bottomLink = new HyperLink();
		LiteralControl topLiteral = new LiteralControl(" ");
		LiteralControl bottomLiteral = new LiteralControl(" ");
		topLink.Text = bottomLink.Text = i.ToString();
		if (_pageIdx != i)
		{
			topLink.NavigateUrl = bottomLink.NavigateUrl = String.Format("~/Default.aspx?pageidx={0}", i);
		}

		TopPager_Panel.Controls.Add(topLink);
		TopPager_Panel.Controls.Add(topLiteral);
		BottomPager_Panel.Controls.Add(bottomLink);
		BottomPager_Panel.Controls.Add(bottomLiteral);
	}

	if (showEndElipticalLink)
	{
		if (_showTopPager)
		{
			AddPagerLink(this.TopPager_Panel, endIdx + 1, "..", "Next Page", true, true, false);
		}

		if (_showBottomPager)
		{
			AddPagerLink(this.BottomPager_Panel, endIdx + 1, "..", "Next Page", true, true, false);
		}
	}

	if (showLastPageLink)
	{
		if (_showTopPager)
		{
			AddPagerLink(this.TopPager_Panel, _totalPages, "Last Page", "Last Page", true, true, false);
		}

		if (_showBottomPager)
		{
			AddPagerLink(this.BottomPager_Panel, _totalPages, "Last Page", "Last Page", true, true, false);
		}
	}
}							
							

Sample Project

The attached sample is extension of code that I did in first sample. The attached sample was created in Visual Studio 2008. We did not use any API from .Net 3.5 or things like that. So if you want to simply port the code over to Visual Studio 2005 or Visual Studio 2003 project, simple copy the page and code file in your project and you should be good to go.

Go Freelance
Home     About us     Contact us    Copyright    Privacy Policy    Return Policy    Advertisers
Copyright © Netomatix