1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
| using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace GlobalWeb.Controls
{
/// <summary>
/// Summary description for FullGridPager
/// FullGridPager is a very nice and complete custom pager to use inside a GridView's PagerTemplate.
/// Images files:
/// Images/firstpage.gif
/// Images/prevpage.gif
/// Images/nextpage.gif
/// Images/lastpage.gif
/// CSS:
/// .pagerOuterTable : the style of the html table.
/// .pageCounter : the style of the first table cell "Page 1 of N".
/// .pageFirstLast : the style of the First and Last cells.
/// .pagerLink : the style of the page number links.
/// .pagePrevNextNumber : the style of the Previous, Next and page Number cells.
/// .pageCurrentNumber : the style of the currently selected page number link.
/// .pageGroups : the style of the page groups cell.
/// Parameters:
/// MaxVisiblePageNumbers : the number of VISIBLE page links.
/// PageCounterText : the text to display at the beginning of the page counter.
/// PageCounterTotalText : the text to display before the count of pages inside the page counter.
/// TheGrid : the gridview control.
///
/// </summary>
public class FullGridPager
{
private const int DEFAULT_MAX_VISIBLE = 0;
private const string DEFAULT_COUNTER_TEXT = "Σελίδα";
private const string DEFAULT_TOTAL_TEXT = "από";
protected int maxVisiblePageNumbers;
protected int firstPageNumber;
protected int lastPageNumber;
protected string pageCounterText;
protected string pageCounterTotalText;
protected GridView theGrid;
protected GridViewRow theGridViewRow;
public FullGridPager(GridView TheGrid)
{
// Default Constructor
maxVisiblePageNumbers = DEFAULT_MAX_VISIBLE;
pageCounterText = DEFAULT_COUNTER_TEXT;
pageCounterTotalText = DEFAULT_TOTAL_TEXT;
theGrid = TheGrid;
}
public FullGridPager(GridView TheGrid, int MaxVisible, string CounterText, string TotalText)
{
// Parameterized constructor
maxVisiblePageNumbers = MaxVisible;
pageCounterText = CounterText;
pageCounterTotalText = TotalText;
theGrid = TheGrid;
}
public int MaxVisiblePageNumbers
{
get { return maxVisiblePageNumbers; }
set { maxVisiblePageNumbers = value; }
}
public string PageCounterText
{
get { return pageCounterText; }
set { pageCounterText = value; }
}
public string PageCounterTotalText
{
get { return pageCounterTotalText; }
set { pageCounterTotalText = value; }
}
public GridView TheGrid
{
get { return theGrid; }
}
public void CreateCustomPager(GridViewRow PagerRow)
{
// Create custom pager inside the Grid's pagerTemplate.
// An html table is used to format the custom pager.
// No data to display.
if (PagerRow == null) return;
theGridViewRow = PagerRow;
HtmlTable pagerInnerTable = (HtmlTable)PagerRow.Cells[0].FindControl("pagerInnerTable");
if (pagerInnerTable != null)
{
// default dynamic cell position.
// (after the pageCount, the "First" and the "Previous" cells).
int insertCellPosition = 2;
if (theGrid.PageIndex == 0)
{
// The first page is currently displayed.
// Hide First and Previous page navigation.
pagerInnerTable.Rows[0].Cells[1].Visible = false;
pagerInnerTable.Rows[0].Cells[2].Visible = false;
// Change the default dynamic cell to 1.
insertCellPosition = 0;
}
CalcFirstPageNumber();
CalcLastPageNumber();
CreatePageNumbers(pagerInnerTable, insertCellPosition);
int lastCellPosition = pagerInnerTable.Rows[0].Cells.Count - 1;
if (theGrid.PageIndex == theGrid.PageCount - 1)
{
// The last page is currently displayed.
// Hide Next and Last page navigation.
pagerInnerTable.Rows[0].Cells[lastCellPosition - 1].Visible = false;
pagerInnerTable.Rows[0].Cells[lastCellPosition].Visible = false;
}
UpdatePageCounter(pagerInnerTable);
}
}
private void CreatePageNumbers(HtmlTable pagerInnerTable, int insertCellPosition)
{
for (int i = firstPageNumber, pos = 1; i <= lastPageNumber; i++, pos++)
{
// Create a new table cell for every data page number.
HtmlTableCell tableCell = new HtmlTableCell();
if (theGrid.PageIndex == i - 1)
tableCell.Attributes.Add("class", "pageCurrentNumber");
else
tableCell.Attributes.Add("class", "pagePrevNextNumber");
// Create a new LinkButton for every data page number.
LinkButton lnkPage = new LinkButton();
lnkPage.ID = "Page" + i.ToString();
lnkPage.Text = i.ToString();
lnkPage.CommandName = "Page";
lnkPage.CommandArgument = i.ToString();
lnkPage.CssClass = "pagerLink";
// Place link inside the table cell; Add the cell to the table row.
tableCell.Controls.Add(lnkPage);
pagerInnerTable.Rows[0].Cells.Insert(insertCellPosition + pos, tableCell);
}
}
private void CalcFirstPageNumber()
{
// Calculate the first, visible page number of the pager.
firstPageNumber = 1;
if (MaxVisiblePageNumbers == DEFAULT_MAX_VISIBLE)
MaxVisiblePageNumbers = theGrid.PageCount;
if (theGrid.PageCount > MaxVisiblePageNumbers)
{
// Seperate page numbers in groups if necessary.
if ((theGrid.PageIndex + 1) > maxVisiblePageNumbers)
{
// Calculate the group to display.
// Example:
// GridView1.PageCount = 12
// maxVisiblePageNumbers = 4
// GridView1.PageIndex+1 = 7
// --> pageGroup = 2 (Page numbers: 5, 6, 7, 8)
decimal pageGroup = Math.Ceiling((decimal)(theGrid.PageIndex + 1) / MaxVisiblePageNumbers);
// Calculate the first page number for the group to display.
// Example :
// if pageGroup = 1 (Page numbers: 1,2,3,4) --> firstPageNumber = 1
// if pageGroup = 2 (Page numbers: 5,6,7,8) --> firstPageNumber = 5
// if pageGroup = 3 (Page numbers: 9,10,11,12) --> firstPageNumber = 9
firstPageNumber = (int)(1 + (MaxVisiblePageNumbers * (pageGroup - 1)));
}
}
}
private void CalcLastPageNumber()
{
// Calculate the last, visible page number of the pager.
lastPageNumber = theGrid.PageCount;
if (MaxVisiblePageNumbers == DEFAULT_MAX_VISIBLE)
MaxVisiblePageNumbers = theGrid.PageCount;
if (theGrid.PageCount > MaxVisiblePageNumbers)
{
lastPageNumber = firstPageNumber + (MaxVisiblePageNumbers - 1);
if (theGrid.PageCount < lastPageNumber)
lastPageNumber = theGrid.PageCount;
}
}
private void UpdatePageCounter(HtmlTable pagerInnerTable)
{
// Display current page number and total number of pages.
Label pageCounter = (Label)pagerInnerTable.Rows[0].Cells[0].FindControl("lblPageCounter");
pageCounter.Text = " " + PageCounterText + " " + (theGrid.PageIndex + 1).ToString() + " " + PageCounterTotalText + " " + theGrid.PageCount.ToString() + " ";
}
}
} |
Partager