Adding Controls To ASP.NET GridView Pager Row

September 14th, 2006 Leave a comment Go to comments

ASP.NET 2.0 GridView control contains the AllowPaging property. Setting this property to a value of true enables the paging feature of the GridView and displays data split across multiple pages if the number of rows in the underlying data source is greater than the PageSize property. By default ASP.NET automatically displays numeric paging controls at the bottom of the GridView.

While automatic paging is a very handy feature of GridView, if you want to display any additional paging controls than those inherently supported by automatic paging, you must resort to specifying your own PagerTemplate and write some additional code to preform the same functionality that GridView would otherwise handle for you.

In certain situations, a better option, however, is to simply inject your custom control(s) into the pager generated by the GridView. The following example shows how we can inject a “View All” LinkButton into GridView’s pager row that allows users to view all rows in the underlying data source at the same time:

protected void MyGridView_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Pager)
    {
        LiteralControl space = new LiteralControl(" ");

        LinkButton lb = new LinkButton();
        lb.ID = "ViewAllLinkButton";
        lb.Text = "View All";
        lb.SkinID = "ProfessionalGridViewPagerViewAll";
        lb.Click += new EventHandler(ViewAllLinkButton_Click);

        // Pager is rendered in a single cell as a table;
        // each page # is in a cell by it's own
        Table table = e.Row.Cells[0].Controls[0] as Table;

        // Add ViewAll linkbutton to the last cell
        TableCell parentCell = table.Rows[0].Cells[table.Rows[0].Cells.Count - 1];
        parentCell.Controls.Add(space);
        parentCell.Controls.Add(lb);
    }
}


protected void ViewAllLinkButton_Click(object sender, EventArgs e)
{
    MyGridView.AllowPaging = false;
}

  1. Anonymous
    February 4th, 2008 at 23:30 | #1

    Thank you very much for posting & sharing the codes. What I love about it, is…the code works! I’ve been looking at how I can implement this.

  2. Anonymous
    February 12th, 2009 at 02:26 | #2

    Thanks for this post.

    One step further, what do I do, if the user wants paging back?

    reply soon

  3. Anonymous
    February 12th, 2009 at 02:58 | #3

    Thanks for the post. You helped me a big deal!

  4. Christine Price
    January 9th, 2013 at 16:25 | #4

    Very useful. One change I suggest is that, on line 16, you add (sorry, the code I’m working with is in VB, woo legacy): table.Rows(0).Cells.Add(New TableCell) This makes it look slightly better, because if your CSS is like mine, each number is in its own little box with a grey outline, and this gives the View All link its own gray outlined box.

  1. No trackbacks yet.
 

Comment moderation is enabled. Your comment may take some time to appear.