28 December 2008

GridView Custom Pager Template

This is a Custom GridView Pager Template with DropDownList for pagination , Next-Previous, First-Last navigation and TextBox with save button to control the Page Size.
I had searched many site for this functionality. Finally after referring a few good examples I came up with the following according to my need.


<div id="pager" >
<asp:DropDownList ID="ddlPageSelector" runat="server" AutoPostBack="true">
<asp:TextBox ID="txtPageSize"
runat="server" Width="25px" EnableViewState="true"></asp:TextBox>

results per page <asp:LinkButton ID="lnkSavePageSize" runat="server"
<asp:Label ID="lblNumber" runat="server"></asp:Label> |

<asp:LinkButton Text="First" CommandName="Page" CommandArgument="First" runat="server" ID="btnFirst" />

<asp:LinkButton Text="Previous" CommandName="Page" CommandArgument="Prev" runat="server" ID="btnPrevious" /> -

<asp:LinkButton Text="Next" CommandName="Page" CommandArgument="Next" runat="server" ID="btnNext" />

<asp:LinkButton Text="Last" CommandName="Page" CommandArgument="Last" runat="server" ID="btnLast" />


******************************CODE BEHIND************************
//need to bind the following event with your gridview control

protected void myGridView_RowCreated(object sender, GridViewRowEventArgs e)
if (e.Row.RowType == DataControlRowType.Pager)

SetPagerButtonStates(myGridView, e.Row, this);


//This is a custom function
public void SetPagerButtonStates(GridView gridView, GridViewRow gvPagerRow, Page page)

int pageIndex = gridView.PageIndex;
int pageCount = gridView.PageCount;

LinkButton btnFirst = (LinkButton)gvPagerRow.FindControl("btnFirst");
LinkButton btnPrevious = (LinkButton)gvPagerRow.FindControl("btnPrevious");
LinkButton btnNext = (LinkButton)gvPagerRow.FindControl("btnNext");
LinkButton btnLast = (LinkButton)gvPagerRow.FindControl("btnLast");
Label lblNumber = (Label)gvPagerRow.FindControl("lblNumber");
TextBox txtPageSize = (TextBox)gvPagerRow.FindControl("txtPageSize");
lblNumber.Text = "Pages " + Convert.ToString(pageIndex+1) + " of " + pageCount.ToString();

btnFirst.Enabled = btnPrevious.Enabled = (pageIndex != 0);
btnNext.Enabled = btnLast.Enabled = (pageIndex < (pageCount - 1));

if (btnNext.Enabled == false)
DropDownList ddlPageSelector =

for (int i = 1; i <= gridView.PageCount; i++)



ddlPageSelector.SelectedIndex = pageIndex;
txtPageSize.Text = gridView.PageSize.ToString();

//Used delegates over here
ddlPageSelector.SelectedIndexChanged += delegate

gridView.PageIndex = ddlPageSelector.SelectedIndex;



protected void lnkSavePageSize_click(object sender, EventArgs e)
GridViewRow pagerRow = myGridView.BottomPagerRow;
TextBox temp1= (TextBox)pagerRow.FindControl("txtPageSize");
myGridView.PageSize = Convert.ToInt32(temp1.Text);


Link for Reference:Click Here


Anonymous said...

This is cool dude.... used with out any problem....!!!

Penny said...

awesome men! THanks. Saved me a ton of hours!

Alan said...

Awesome! I was just about to have to create exactly this. Thanks for the time savings!

Anonymous said...

Great! Just what I needed