The Black Knight Sings

Songs about SharePoint and other adventures by Per Jakobsen


Date: # Friday, December 19, 2008

Title: Custom pagers for SPGridView


I don't think that the built in pagers for SPGridView are the most useful and informative, so for one of my current projects I decided to implement two custom pagers

The first was XofYPager which shows the page numbers like this:


The second was SmartPager which shows the page numbers like this:


The use of these pagers are very simple just include the classes below and then before you DataBind() your SPGridView set the PagerTemplate property to an instance of one of these classes.

The XofYPager's constructor should have two parameters:
  format: which is a string like "{0} of {1}" where {0} will be replaced by current page and {1} by the page count
  grid: which is a reference to the SPGridView

    1     class XofYPager : ITemplate

    2     {

    3         GridView _grid;

    4         String _format;

    5         public XofYPager(string format, GridView grid)

    6         {

    7             _format = format;

    8             _grid = grid;

    9 

   10         }

   11         public void InstantiateIn(Control container)

   12         {

   13             Table tbl = new Table();

   14             container.Controls.Add(tbl);

   15             tbl.Width = Unit.Percentage(100);

   16             TableRow row = new TableRow();

   17             tbl.Rows.Add(row);

   18             TableCell cell = new TableCell();

   19             row.Cells.Add(cell);

   20             cell.HorizontalAlign = HorizontalAlign.Center;

   21 

   22             int currentPage = _grid.PageIndex + 1;

   23             if (currentPage > 1)

   24             {

   25                 ImageButton prevBtn = new ImageButton();

   26                 prevBtn.ImageUrl = "~/_layouts/images/prev.gif";

   27                 prevBtn.CommandName = "Page";

   28                 prevBtn.CommandArgument = "Prev";

   29                 cell.Controls.Add(prevBtn);

   30             }

   31             cell.Controls.Add(new LiteralControl(String.Format(_format, currentPage, _grid.PageCount)));

   32             if (currentPage < _grid.PageCount)

   33             {

   34                 ImageButton nextBtn = new ImageButton();

   35                 nextBtn.ImageUrl = "~/_layouts/images/next.gif";

   36                 nextBtn.CommandName = "Page";

   37                 nextBtn.CommandArgument = "Next";

   38                 cell.Controls.Add(nextBtn);

   39             }

   40         }

   41     }


The SmartPager's constructor should have two parameters:
  additionalpages: which is the number of pages to show on each side of the current page (should be >= 1, 2 is used in the example above)
  grid: which is a reference to the SPGridView

    1     class SmartPager : ITemplate

    2     {

    3         GridView _grid;

    4         int _additionalpages;

    5         public SmartPager(int additionalpages, GridView grid)

    6         {

    7             _additionalpages = additionalpages;

    8             _grid = grid;

    9 

   10         }

   11         public void InstantiateIn(Control container)

   12         {

   13             Table tbl = new Table();

   14             container.Controls.Add(tbl);

   15             tbl.Width = Unit.Percentage(100);

   16             TableRow row = new TableRow();

   17             tbl.Rows.Add(row);

   18             TableCell cell = new TableCell();

   19             row.Cells.Add(cell);

   20             cell.HorizontalAlign = HorizontalAlign.Center;

   21 

   22             int currentPage = _grid.PageIndex + 1;

   23             if (_grid.PageCount < (5+_additionalpages*2)+1)

   24             {

   25                 AddPages(cell,1,_grid.PageCount,currentPage);

   26             }

   27             else

   28             {

   29                 if (currentPage < 4+_additionalpages)

   30                 {

   31                     AddPages(cell,1,3+_additionalpages*2,currentPage);

   32                     cell.Controls.Add(new LiteralControl("..."));

   33                     AddPages(cell,_grid.PageCount,_grid.PageCount,currentPage);

   34                 }

   35                 else

   36                 {

   37                     AddPages(cell,1,1,currentPage);

   38                     cell.Controls.Add(new LiteralControl("..."));

   39                     if (currentPage>_grid.PageCount-3-_additionalpages)

   40                         AddPages(cell,_grid.PageCount-2-_additionalpages*2,_grid.PageCount,currentPage);

   41                     else

   42                     {

   43                         AddPages(cell,currentPage-_additionalpages,currentPage+_additionalpages,currentPage);

   44                         cell.Controls.Add(new LiteralControl("..."));

   45                         AddPages(cell,_grid.PageCount,_grid.PageCount,currentPage);

   46                     }

   47                 }

   48             }

   49         }

   50 

   51         void AddPages(Control container, int from, int to, int current)

   52         {

   53             for (int i = from; i <= to; i++)

   54             {

   55                 LinkButton link=new LinkButton();

   56                 link.CommandName="Page";

   57                 link.CommandArgument=i.ToString();

   58                 link.Text = i.ToString();

   59                 if (i == current)

   60                     link.Style.Add(HtmlTextWriterStyle.FontWeight,"700");

   61                 container.Controls.Add(link);

   62                 if (i<to)

   63                     container.Controls.Add(new LiteralControl("|"));

   64             }

   65         }

   66     }



Friday, December 19, 2008 10:16:11 PM (Romance Standard Time, UTC+01:00)  #    Comments [2]

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way. And all information or programs are without warranty. Use at your own risk


© Copyright 2010 Send mail to the author(s) Per Jakobsen Feed your aggregator (RSS 2.0)