Skip to content

Instantly share code, notes, and snippets.

@arviman
Created November 2, 2011 19:48
Show Gist options
  • Save arviman/1334681 to your computer and use it in GitHub Desktop.
Save arviman/1334681 to your computer and use it in GitHub Desktop.
/// <summary>
/// sets the paging control's properties
/// </summary>
protected void FormatPagingControl()
{
//number of page links
int n = 4;
int lastpage = ((RowCount -1) / itemsPerPage) + 1;
int startPage = CurrentPage;
int lastPageToDisplay = Math.Min(lastpage, startPage + (n-1));
//if we have only page, do not show paging controls
if (RowCount <= itemsPerPage)
{
plcPaging.Visible = false;
return;
}
else plcPaging.Visible = true;
//in the case that current page is not in the first n pages and belongs to last (n-1) pages
//current page will not be the first page link.
int numberOfPagesDisplayed = lastPageToDisplay - startPage + 1;
//make sure we display maximum number of page links as possible.
if(numberOfPagesDisplayed < n)
{
//push start page back
startPage -= (n - numberOfPagesDisplayed);
}
if (startPage < 1)
startPage = 1;
/*
* Now, using startpage and lastPageToDisplay,perform render
* */
//previous and next buttons
prevLnk.Visible = (startPage > 1);
nextLnk.Visible = (lastPageToDisplay < lastpage);
//page links and spacers
for (int i = 1; i <= n; ++i)
{
LinkButton lnk = ((plcPaging.FindControl("lnkPage" + i)) as LinkButton);
Label spacer = ((plcPaging.FindControl("spacer" + i)) as Label);
int displayNum = startPage + i - 1;
//visible if page's number is less than or equal to last page to display
spacer.Visible = lnk.Visible = displayNum <= lastPageToDisplay;
//make final spacer invisible
if (i != 1 && lnk.Visible == false)
((plcPaging.FindControl("spacer" + (i - 1).ToString())) as Label).Visible = false;
//set text
lnk.Text = displayNum.ToString();
//grey out if page represents current page
lnk.Enabled = (CurrentPage != displayNum);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment