在.net中,除了人手动写分页类之外,微软还提供了官方的分页,分页工具类包括PagedList和PagerExtension,先来看看PagedList类里面是怎么写的:
PagedList
using System;
using System.Collections.Generic;
using System.Linq;
namespace System.Web.Mvc
{
public class PagedList<T> : List<T>
{
/// <summary>
/// 页索引
/// </summary>
public int PageIndex { get; private set; }
/// <summary>
/// 页大小
/// </summary>
public int PageSize { get; private set; }
/// <summary>
/// 总数据条数
/// </summary>
public int TotalCount { get; private set; }
/// <summary>
/// 总页数
/// </summary>
public int TotalPages { get; private set; }
/// <summary>
/// 数据信息
/// </summary>
// public List<T> Source { get; private set; }
public PagedList(List<T> source, int pageIndex, int pageSize, int totalCount)
{
PageIndex = pageIndex;
PageSize = pageSize;
TotalCount = totalCount;
TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);
this.AddRange( source.Take(source.Count));
}
public PagedList()
{}
/// <summary>
/// 是否包含上一页
/// </summary>
public bool HasPreviousPage
{
get
{
return (PageIndex > 1);
}
}
public bool HasNextPage
{
get
{
return (PageIndex < TotalPages);
}
}
}
}