MVC做分页功能
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace JPJC.Controllers
{
///
/// 分頁類
///
/// 集合
public class Page : List
{
///
/// 索引頁
///
public int PageIndex { get; set; }
///
/// 每頁顯示幾條記錄
///
public int PageSize { get; set; }
///
/// 一共幾頁
///
public int PageCount { get; set; }
///
/// 一共多少條記錄
///
public int AllCount { get; set; }
///
/// 判斷上一頁
///
public bool HasPrevious
{
get
{
return PageIndex < PageCount - 1;
}
}
///
/// 判斷下一頁
///
public bool HasNext
{
get
{
return PageIndex > 0;
}
}
///
/// 翻頁函數
///
/// IQueryable 數據
/// 索引頁
/// 每頁多少條
public Page(IQueryable source, int pageindex, int pagesize)
{
this.AllCount = source.Count();
this.PageIndex = pageindex;
this.PageSize = pagesize;
//總記錄數與每頁最大記錄數求余
if (this.AllCount % this.PageSize == 0)
this.PageCount = Convert.ToInt32(this.AllCount / this.PageSize);
else
this.PageCount = Convert.ToInt32(this.AllCount / this.PageSize + 1);
if (this.PageIndex > this.PageCount) this.PageIndex = this.PageCount;
if (this.PageIndex < 1) this.PageIndex = 1;
var list = source.Skip(this.PageSize * (this.PageIndex - 1)).Take(this.PageSize);
this.AddRange(list);
}
}
}
這是分頁類
使用方法
var Query = from t in context.DB_Users
select t;
Query = Query.OrderByDescending(p => p.UserID);
var list = new Page(Query, Page, PageSize);
翻頁類
///?
/// 顯示分頁信息
///?
/// 每頁多少條
/// 當前頁
/// 總頁數
/// 總計路數
/// 記錄單位
/// 記錄名稱
/// 翻頁函數javascirpt
/// 是否顯示跳轉按鈕
///?
public static string ShowPages(int PageSize, int PageIndex, int PageCount, int AllCount, string Unit, string ItemName, bool JumpPageButton)
{
string tempstr = "";
int jump = PageIndex + 1;
tempstr += " 共?" + AllCount + "?" + Unit + ItemName + " ";
tempstr += "?" + PageSize + "條/頁 ";
tempstr += " 頁次:" + PageIndex + "/" + PageCount + "頁 ";
if (PageIndex < 2)
{
tempstr += "首頁?上一頁?";
}
else
{
tempstr += "首頁?";
tempstr += "上一頁?";
}
if (PageIndex >= PageCount)
{
tempstr += "下一頁?尾頁";
jump = PageCount;
}
else
{
tempstr += "下一頁?";
tempstr += "尾頁?";
}
tempstr += "?";
if (JumpPageButton)
{
tempstr += "?";
tempstr += "?跳轉";
if (PageCount == 1)
{
tempstr += "
}
else
{
tempstr += "
}
}
tempstr += " ";
return tempstr;
}
使用方法
ShowPages(PageSize, Page, list.PageCount, list.AllCount, "個", "用戶", true)
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace JPJC.Controllers
{
///
/// 分頁類
///
/// 集合
public class Page : List
{
///
/// 索引頁
///
public int PageIndex { get; set; }
///
/// 每頁顯示幾條記錄
///
public int PageSize { get; set; }
///
/// 一共幾頁
///
public int PageCount { get; set; }
///
/// 一共多少條記錄
///
public int AllCount { get; set; }
///
/// 判斷上一頁
///
public bool HasPrevious
{
get
{
return PageIndex < PageCount - 1;
}
}
///
/// 判斷下一頁
///
public bool HasNext
{
get
{
return PageIndex > 0;
}
}
///
/// 翻頁函數
///
/// IQueryable 數據
/// 索引頁
/// 每頁多少條
public Page(IQueryable source, int pageindex, int pagesize)
{
this.AllCount = source.Count();
this.PageIndex = pageindex;
this.PageSize = pagesize;
//總記錄數與每頁最大記錄數求余
if (this.AllCount % this.PageSize == 0)
this.PageCount = Convert.ToInt32(this.AllCount / this.PageSize);
else
this.PageCount = Convert.ToInt32(this.AllCount / this.PageSize + 1);
if (this.PageIndex > this.PageCount) this.PageIndex = this.PageCount;
if (this.PageIndex < 1) this.PageIndex = 1;
var list = source.Skip(this.PageSize * (this.PageIndex - 1)).Take(this.PageSize);
this.AddRange(list);
}
}
}
這是分頁類
使用方法
var Query = from t in context.DB_Users
select t;
Query = Query.OrderByDescending(p => p.UserID);
var list = new Page(Query, Page, PageSize);
翻頁類
///?
/// 顯示分頁信息
///?
/// 每頁多少條
/// 當前頁
/// 總頁數
/// 總計路數
/// 記錄單位
/// 記錄名稱
/// 翻頁函數javascirpt
/// 是否顯示跳轉按鈕
///?
public static string ShowPages(int PageSize, int PageIndex, int PageCount, int AllCount, string Unit, string ItemName, bool JumpPageButton)
{
string tempstr = "";
int jump = PageIndex + 1;
tempstr += " 共?" + AllCount + "?" + Unit + ItemName + " ";
tempstr += "?" + PageSize + "條/頁 ";
tempstr += " 頁次:" + PageIndex + "/" + PageCount + "頁 ";
if (PageIndex < 2)
{
tempstr += "首頁?上一頁?";
}
else
{
tempstr += "首頁?";
tempstr += "上一頁?";
}
if (PageIndex >= PageCount)
{
tempstr += "下一頁?尾頁";
jump = PageCount;
}
else
{
tempstr += "下一頁?";
tempstr += "尾頁?";
}
tempstr += "?";
if (JumpPageButton)
{
tempstr += "?";
tempstr += "?跳轉";
if (PageCount == 1)
{
tempstr += "
}
else
{
tempstr += "
}
}
tempstr += " ";
return tempstr;
}
使用方法
ShowPages(PageSize, Page, list.PageCount, list.AllCount, "個", "用戶", true)
轉載于:https://www.cnblogs.com/ziye16888/archive/2013/05/22/3092104.html
總結
- 上一篇: c语言中的常用符号
- 下一篇: RIA开发权威指南 基于JavaFX(赠