Introduction
GridView has no built-in pager mode that allows you to display both next/previous page links as well as page numbers.
I have no idea why. But it's really easy to implement.
Background
Once I had requirement to do this, I've found several solutions over the network but all of them were to complex. Creating several files,
catching bugs, injecting code at several points of control flow... uff, why so difficult? GridView control has already the possibility to display
page numbers and ALL that I need was just add "<<" and ">>" buttons! If this is your case feel free to use my code.
Using the code
0. Implement common paging using "Numeric" type of pager. My code expects pager in the bottom position but it's easy to make changes if you want to use it on top.
1. Your GridView should bind the "DataBound
" event: OnDataBound="gv_DataBound"
.
2. Use this code:
protected void gv_DataBound(object sender, EventArgs e)
{
EnableNextPrevNavigationForNumericPagedGrid(gv);
}
private static void EnableNextPrevNavigationForNumericPagedGrid(GridView gv)
{
if (gv.BottomPagerRow == null)
return;
Table pagerTable = (Table)gv.BottomPagerRow.Controls[0].Controls[0];
bool prevAdded = false;
if (gv.PageIndex != 0)
{
TableCell prevCell = new TableCell();
LinkButton prevLink = new LinkButton
{
Text = "<<",
CommandName = "Page",
CommandArgument = ((LinkButton)pagerTable.Rows[0].Cells[gv.PageIndex - 1].Controls[0]).CommandArgument
};
prevCell.Controls.Add(prevLink);
pagerTable.Rows[0].Cells.AddAt(0, prevCell);
prevAdded = true;
}
if (gv.PageIndex != gv.PageCount - 1)
{
TableCell nextCell = new TableCell();
LinkButton nextLink = new LinkButton
{
Text = ">>",
CommandName = "Page",
CommandArgument = ((LinkButton)pagerTable.Rows[0].Cells[gv.PageIndex +
(prevAdded ? 2 : 1)].Controls[0]).CommandArgument
};
nextCell.Controls.Add(nextLink);
pagerTable.Rows[0].Cells.Add(nextCell);
}
}
Paging works in normal way. The only thing that my code does is adding "<<" and ">>" buttons. "<<" button won't appear
at the first page as well as ">>" at the last one.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.