PagedList.cs

    1 /*
    2  ASP.NET MvcPager control
    3  Copyright:2009-2010 Webdiyer (http://en.webdiyer.com)
    4  Source code released under Ms-PL license
    5  */
    1 using System;
    2 using System.Collections.Generic;
    3 
    4 namespace Webdiyer.WebControls.Mvc
    5 {
    6     public class PagedList<T> : List<T>
    7     {
    8         public PagedList(IList<T> items,int pageIndex,int pageSize)
    9         {
   10             PageSize = pageSize;
   11             TotalItemCount = items.Count;
   12             TotalPageCount = (int)Math.Ceiling(TotalItemCount / (double)PageSize);
   13             CurrentPageIndex = pageIndex;
   14             StartRecordIndex=(CurrentPageIndex - 1) * PageSize + 1;
   15             EndRecordIndex = TotalItemCount > pageIndex * pageSize ? pageIndex * pageSize : TotalItemCount;
   16             for (int i = StartRecordIndex-1; i < EndRecordIndex;i++ )
   17             {
   18                 Add(items[i]);
   19             }
   20         }
   21 
   22         public PagedList(IEnumerable<T> items, int pageIndex, int pageSize, int totalItemCount)
   23         {
   24             AddRange(items);
   25             TotalItemCount = totalItemCount;
   26             TotalPageCount = (int)Math.Ceiling(totalItemCount / (double)pageSize);
   27             CurrentPageIndex = pageIndex;
   28             PageSize = pageSize;
   29             StartRecordIndex = (pageIndex - 1) * pageSize + 1;
   30             EndRecordIndex = TotalItemCount > pageIndex * pageSize ? pageIndex * pageSize : totalItemCount;
   31         }
   32 
   33         public int CurrentPageIndex { get; set; }
   34         public int PageSize { get; set; }
   35         public int TotalItemCount { get; set; }
   36         public int TotalPageCount{get; private set;}
   37         public int StartRecordIndex{get; private set;}
   38         public int EndRecordIndex{get; private set;}
   39     }
   40 }