SPGridView的分页控件
由于SPGridView中自带的分页样式很普通,所以编写一个分页的Template,主要是仿照SharePoint中的分页效果编写的,实现ITemplate接口,添加我们需要的控件,并且调整显示的样式,实现如下:
public class SPGridViewPagerTemplate : ITemplate
{
#region Fields
private SPGridView mGrid = null;
private string mFormat = string.Empty;
#endregion
#region Methods
/// <summary>
/// Constructor
/// </summary>
/// <param name="format"></param>
/// <param name="grid"></param>
public SPGridViewPagerTemplate(string format, SPGridView grid)
{
mFormat = format;
mGrid = grid;
}
/// <summary>
/// Instantiat in the control to container
/// </summary>
/// <param name="container"></param>
public void InstantiateIn(Control container)
{
try
{
Table tbl = new System.Web.UI.WebControls.Table();
container.Controls.Add(tbl);
tbl.Width = Unit.Percentage(100);
TableRow row = new TableRow();
tbl.Rows.Add(row);
TableCell cell = new TableCell();
cell.Font.Size = new FontUnit(FontSize.Smaller);
row.Cells.Add(cell);
cell.HorizontalAlign = HorizontalAlign.Center;
int currentPage = mGrid.PageIndex + 1;
int from = 0;
int to = 0;
if (currentPage > 1)
{
ImageButton prevBtn = new ImageButton();
prevBtn.ImageUrl = "~/_layouts/images/prev.gif";
prevBtn.CommandName = "Page";
prevBtn.CommandArgument = "Prev";
cell.Controls.Add(prevBtn);
}
if (currentPage > 1)
{
if (currentPage == mGrid.PageCount)
{
from = (currentPage - 1) * mGrid.PageSize + 1;
to = mGrid.Rows.Count + (currentPage - 1) * mGrid.PageSize;
}
else
{
from = (currentPage - 1) * mGrid.PageSize + 1;
to = from + mGrid.PageSize - 1;
}
}
else
{
from = 1;
to = mGrid.PageSize;
}
LiteralControl lControl = new LiteralControl(String.Format(mFormat, from, to));
cell.Controls.Add(lControl);
if (currentPage < mGrid.PageCount)
{
ImageButton nextBtn = new ImageButton();
nextBtn.ImageUrl = "~/_layouts/images/next.gif";
nextBtn.CommandName = "Page";
nextBtn.CommandArgument = "Next";
cell.Controls.Add(nextBtn);
}
}
catch (Exception ex)
{
}
}
#endregion
}