. . . .

Numeric First Last with Next & Previous

posted 24 April 2007
Hello everyone (Im the only reader?)
Long time since I last posted as I have not encountered anything that I felt was worth a post so that explains the lengthy duration.

Anyhow as the title suggests:
.Net GridView PagerSettings by default give you 4 options most useful , yet at times you may want something extra; such as a Numeric First/Last with previous and next paging ability. I was tasked with this and couldn't find anything that was not overly complex (required a custom pager etc). I found one blog (link to come if i can find it again) that had some customization taking place during the RowCreated Event. After thinking about it I threw it together.

First Set the GridView PagerSettings to "NumericFirstLast"
this will give you a pager that resembles the following:
1 ... 5 6 7 ... 10, where the "...",s will increment to the n number of documents.

Next wire up an event handler for the OnRowCreated Event. The following code explains the rest:

switch (e.Row.RowType)
case DataControlRowType.Pager:
Table pagerTable = (Table)e.Row.Cells[0].Controls[0];
TableRow row = pagerTable.Rows[0];
if (row.Cells.Count > 1)
LinkButton prev = row.Cells[1].Controls[0] as LinkButton;
if (prev != null && prev.Text == "...")
prev.Text = "<<; prev.CommandArgument = "Prev"; } LinkButton next = row.Cells[row.Cells.Count - 2].Controls[0] as LinkButton; if (next != null &&amp;amp;amp;amp;amp; next.Text == "...") { next.Text = ">>";
next.CommandArgument = "Next";

Basically when the pager row is created, we find the linkbutton on either side that jump pages forwards and backwards (the ...'s ) and reconfigure them to display the appropriate text we want and commandarguments.

Thus giving you a numeric first last and next/previous pager style with minimal work.
The code above resembles the following :
1 << 5 6 7 >> 10

And thats all from me for now. Good, Bad or Ugly ? you be the judge..