asp.net custom gridview paging
Gridview Paging you could go with the standard and avoid the headaches and trial/errors a custom one brings. But sometimes it’s not up to you is it? There’s always that awesome web designer that thinks of an “awesome” paging for your gridviews..and here comes the headaches..lol
So I did a little searching and found a great tutorial that got me started. Here’s the link for that http://www.dotnetcurry.com/ShowArticle.aspx?ID=339
private void SetPaging()
{
//check to see if my gridview has more than 1 page
if (gridview1.PageCount > 1)
{
GridViewRow toprow = gridview1.TopPagerRow;
PlaceHolder topplace = toprow.FindControl("PlaceHolder1") as PlaceHolder;
Button toplbl = toprow.FindControl("lblIndicator") as Button;
toplbl.Text = "Page " + (gridview1.PageIndex + 1).ToString() + " of " + gridview1.PageCount.ToString();
topplace.Controls.Clear();
//amount of visible page buttons
int maxpage = 4;
//starts off at page 1
int i = 1;
//if it within the first 4 pages then sets the max to pagecount
if (gridview1.PageCount = 4)
{
//set i to current page +1 because page index's are 0 based
i = gridview1.PageIndex + 1;
//if current page is greater than the set max page then you have to add on 4 more to your max page so you get then next four pages.
if (i > maxpage)
maxpage = i + 4;
//this validates that if after adding on 4 more pages if your current max page is greater than total pages (page count) then just set maxpage to your pagecount
if (maxpage > gridview1.PageCount)
maxpage = gridview1.PageCount;
}
//this loop creates each page button inside a placeholder
for (int j = i; j 4)
{
btnLastFour.CommandName = "Page";
btnLastFour.CommandArgument = (i - 4).ToString();
btnLastFour.Visible = true;
}
//if you are past the first page then it enables the "first page" button.
if (gridview1.PageIndex > 1)
{
btnFirstPage.CommandName = "Page";
btnFirstPage.CommandArgument = "1";
btnFirstPage.Visible = true;
}
Button btnLastPage = toprow.FindControl("btnLastPage") as Button;
Button btnNextFour = toprow.FindControl("btnNextFour") as Button;
//here it's controled by page count not by page index
//if the page count has more than 4 pages then it enables the "next four" page button
//and also the last page button
if (gridview1.PageCount > 4)
{
btnNextFour.CommandName = "Page";
btnNextFour.CommandArgument = (i + 4).ToString();
btnLastPage.CommandName = "Page";
btnLastPage.CommandArgument = gridview1.PageCount.ToString();
}
else
{
btnNextFour.Visible = false;
lastbtn.Visible = false;
}
//if your current maxpage is same as pagecount the doesn't show because you are at the end of your pages.
if (maxpage == gridview1.PageCount)
{
btnNextFour.Visible = false;
lastbtn.Visible = false;
}
}
}
