这个分页是自定义分页,只从数据库中读取当前的数据,样子看着跟GridView的自动分页基本上完全一样。
1、用两个ViewState保存当前页数CurrentPage和总行数RowCount
#region 分页
/// <summary>
/// 当前页
/// </summary>
private int CurrentPage
{
get
{
if (ViewState["CurrentPage"] == null)
{
return 0; // 页数从0开始
}
else
{
return (int)ViewState["CurrentPage"];
}
}
set
{
ViewState["CurrentPage"] = value;
}
}
/// <summary>
/// 总行数
/// </summary>
private int RowCount
{
get
{
if (ViewState["RowCount"] == null)
{
return 0;
}
else
{
return (int)ViewState["RowCount"];
}
}
set
{
ViewState["RowCount"] = value;
}
}
#endregion
2、加载数据
private void LoadData()
{
。。。。。。
int totalCount = bll.GetRowcount(letter, teamName);// 总行数
RowCount = totalCount;// 保存总行数到ViewState中
// 得到当前页的数据,前两个参数分别是每页显示的记录数和当前是第几页
List<StdTeamInfo> lstStdTeam = bll.GetListByPager(GridView1.PageSize, CurrentPage + 1, letter, teamName);
GridView1.DataSource = lstStdTeam;
GridView1.DataBind();
}
3、加GridView的分页方法,跟自动分页时加分页方法一样
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.CurrentPage = e.NewPageIndex;//保存当前是第几页到ViewState中
LoadData();
}
4、重写OnPreRender,加分页的链接
protected override void OnPreRender(EventArgs e)
{
// Utility.GetPagerString,此方法在第5步介绍,功能是得到分页的链接字符串
string pagerString = Utility.GetPagerString(RowCount, (CurrentPage + 1), GridView1.PageSize, this.GridView1.ClientID);
if (pagerString != "")
{
int index = this.GridView1.Rows.Count;
GridViewRow rowHeader = new GridViewRow(index, index, DataControlRowType.Pager, DataControlRowState.Normal);
TableCell headerCell = new TableCell();
headerCell.ColumnSpan = 8;
headerCell.Text = pagerString;
rowHeader.Cells.Add(headerCell);
this.GridView1.Controls[0].Controls.Add(rowHeader);
}
}
5、GetPagerString方法,得到分页的链接字符串
/// <summary>
/// 返回分页的链接串
/// </summary>
/// <param name="totalCount">总行数</param>
/// <param name="currentPage">当前第几页(从0开始)</param>
/// <param name="pageSize">每页记录数</param>
/// <param name="gridViewID">GridView控件ID</param>
/// <returns></returns>
public static string GetPagerString(int totalCount, int currentPage, int pageSize,
string gridViewID)
{
gridViewID = gridViewID.Replace("_", "$");
// 总页数
int pageCount = (totalCount % pageSize == 0) ? (totalCount / pageSize) : (totalCount / pageSize + 1);
if (pageCount == 1 || pageCount == 0) return "";
int pageFrom = (currentPage - (currentPage % 30 == 0 ? 30 : currentPage % 30)) + 1; // 这里把显示的最大页面数设为了30
if (pageFrom < 0) pageFrom = 1;
int pageTo = currentPage + (30 - (currentPage % 30 == 0 ? 30 : currentPage % 30));
if (pageTo > pageCount) pageTo = pageCount;
StringBuilder sb = new StringBuilder();
sb.Append("<table border=\"0\"><tr>");
if (currentPage > 30)
sb.Append("<td><a href=\"javascript:__doPostBack('" + gridViewID + "','Page$" + (pageFrom - 1) + "')\">...</a></td>");
for (int i = pageFrom; i <= pageTo; i++)
{
if (i == currentPage)
sb.Append("<td><span>" + i + "</span></td>");
else
sb.Append("<td><a href=\"javascript:__doPostBack('" + gridViewID + "','Page$" + i + "')\">" + i + "</a></td>");
}
if (pageTo < pageCount)
sb.Append("<td><a href=\"javascript:__doPostBack('" + gridViewID + "','Page$" + (pageTo + 1) + "')\">...</a></td>");
sb.Append("<td>共" + pageCount + "页</td>");
sb.Append("</tr></table>");
return sb.ToString();
}
至此这个自定义分页完成了。样式跟GridView的自定义分页一样,但是只读出需要的数据,页面显示速度快了不少。