首页 > 代码库 > 几种不同的分页处理办法

几种不同的分页处理办法


--------------------------------------第一种:利用动软代码生成器生成的分页------------------------------------
//新建一个一般处理程序
pageIndex = int.Parse(HttpContext.Current.Request.Params["page"]);//获取到页数
pageSize = int.Parse(HttpContext.Current.Request.Params["rows"]);//获取每页的大小

total = bll.GetRecordCount(" " );//获取总数

DataSet ds = bll.GetListByPage(" ", "ID", (pageIndex - 1) * pageSize + 1, pageSize * pageIndex);//GetRecordCount,GetListByPage都是生成器生成
list = bll.DataTableToList(ds.Tables[0]);//DataTableToList
var data = http://www.mamicode.com/new { total = total, rows = list };//EasyUI中的datagrid返回json数据的格式
//将data集合构造成Json字符串
HttpContext.Current.Response.Write(Kits.SerializeToJson(data));//SerializeToJson是在公共类的一个办法

----------------------------------第二种:利用LaomaPager生成类插件来分页--------------------------------------------------------------------------
--后台页面
protected List<Model.HKSJ_Main> mainShow = new List<Model.HKSJ_Main>();

//实现分页显示图片数据
protected string NavPager { get; set; }

protected void Page_Load(object sender, EventArgs e)
{
//获取网站Main表中的数据
BLL.HKSJ_Main mainServices = new BLL.HKSJ_Main();

//首先获取分页参数
int pageSize = Request["pageSize"] == null ? 10 : Convert.ToInt32(Request["pageSize"]);
int pageIndex = Request["pageIndex"] == null ? 1 : Convert.ToInt32(Request["pageIndex"]);

//获取到totalCount的数据
int totalCount = mainServices.GetRecordCount(string.Empty);

//计算在页面上面显示的分页的数量
DataSet ds = mainServices.GetListByPage(string.Empty, "ID", pageSize * (pageIndex - 1) + 1, pageIndex * pageSize);
//内容数据显示
mainShow = mainServices.DataTableToList(ds.Tables[0]);
//分页数据显示,LaomaPager是封装类插件
NavPager = Common.LaomaPager.ShowPageNavigate(pageSize, pageIndex, totalCount);
}

注释说明:GetListByPage是生成器里面就可以直接用的,LaomaPager是封装类插件(找一个就好)

--前台页面
<div class="pages"><%=NavPager %></div>

--前台页面表格数据
<asp:Content ID="Content2" ContentPlaceHolderID="Content" runat="server">
<table style="width: 100%">
<tr>
<th>ID</th>
<th>title</th>
<th>content</th>
<th>people</th>
<th>date</th>
<th>status</th>
<th>MainPeople</th>
</tr>

<%foreach (var empoyees in Empoyees)
{%>
<tr>
<td><%=empoyees.ID %></td>
<td><%=empoyees.title %></td>
<td><%=empoyees.content %></td>
<td><%=empoyees.people %></td>
<td><%=empoyees.date %></td>
<td><%=empoyees.status %></td>
<td><%=empoyees.MainPeople %></td>
</tr>
<%} %>
</table>

</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="NavPage" runat="server">
<%=NavPager %>
</asp:Content>

--------------------------------------第三种:利用存储过程分页--------------------------------------------------
//新建一个一般处理程序
//接收表格的分页数据
strPageIndex = context.Request.Params["page"];
strPageSize = context.Request.Params["rows"];

PagedData pageData = http://www.mamicode.com/new PagedData() { PageIndex = int.Parse(strPageIndex), PageSize = int.Parse(strPageSize) };//PagedData 是一个公共类,如下面


//调用存储过程来查询数据 并使用out 修改输出参数的值
var workFlowList = workFlowBll.GetPageDataByProcedure(int.Parse(strPageIndex), int.Parse(strPageSize), "IsDel=‘false‘", out rowCount);

//List<NanFang_WorkFlowNode> workFlowNodeList = workFlowNodeBll.GetModelList(" IsDel=‘false‘");

//将必要的数据赋值给EasyUI需要的变量
pageData.rows = workFlowList;
pageData.total = rowCount;

string str=Kits.SerializeToJson(pageData);
context.Response.Write(str);

//PagedData公共类
public class PagedData
{
public int PageIndex;
public int PageSize;
public int total;// RowCount; 是为 EasyUI的datagrid组件 而改
public object rows;//PagedList; 是为 EasyUI的datagrid组件 而改

public int PageCount
{
get
{
return (int)Math.Ceiling(Convert.ToDouble(total) / Convert.ToDouble(PageSize));
}
}
}
//在BLL层添加
public List<NanFang.Model.NanFang_WorkFlow> GetPageDataByProcedure(int pageIndex, int pageSize, string strWhere, out int rowsCount)
{
DataSet ds = dal.GetPageDataByProcedure(pageIndex, pageSize, strWhere, out rowsCount);
return DataTableToList(ds.Tables[0]);
}
//在IDAL层添加
DataSet GetPageDataByProcedure(int PageIndex, int PageSize, string strWhere, out int rowsCount);
//在DAL层添加
public DataSet GetPageDataByProcedure(int PageIndex, int PageSize, string strWhere, out int rowsCount)
{
SqlParameter[] parameters = {
new SqlParameter("@pageIndex", SqlDbType.Int),
new SqlParameter("@pageSize", SqlDbType.Int),
new SqlParameter("@strWhere", SqlDbType.NVarChar,200),
new SqlParameter("@rowCount", SqlDbType.Int)
};
parameters[0].Value = http://www.mamicode.com/PageIndex;
parameters[1].Value = http://www.mamicode.com/PageSize;
parameters[2].Value = http://www.mamicode.com/strWhere;
parameters[3].Direction = ParameterDirection.Output;

return DbHelperSQL.RunProcedure("usp_GetWorkFlowPage", parameters, out rowsCount);//RunProcedure办法是代码生成器自带的
}
//在数据库中写的存储过程

USE [NanFang_Hospital]
GO
/****** Object: StoredProcedure [dbo].[usp_GetWorkFlowPage] Script Date: 04/02/2014 11:13:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[usp_GetWorkFlowPage]
@pageIndex int,--查询的起始页
@pageSize int,--每页多少条数据
@strWhere nvarchar(200),--查询workFlow表的条件
@rowCount int output --输出函数
AS
BEGIN
declare @startNum nvarchar(10),--当前页开始的索引
@endNum nvarchar(10),--当前页结束索引
@sqlCount nvarchar(300),--查询数据的总行数
@sqlSelect nvarchar(300)
set @startNum=cast((@pageIndex-1)*@pageSize as nvarchar(10))--计算当前页开始的索引
set @endNum=cast(@pageIndex*@pageSize as nvarchar(10))--计算当前页结束的索引
--判断是否有条件的查询
if(LEN(@strWhere)>1)
--有条件的查询就执行下面语句
begin
set @sqlCount=N‘select @rowCount=count(ID) from dbo.NanFang_WorkFlow where ‘+@strWhere
set @sqlSelect=‘select k.* from (select ROW_NUMBER() over(order by ID) AS num,* FROM dbo.NanFang_WorkFlow where ‘+@strWhere+‘) as k where k.num>‘+@startNum+‘ and k.num<=‘+@endNum+‘ order by k.AddTime asc ‘
end

else
begin
set @sqlCount=N‘select @rowCount=count(ID) from dbo.NanFang_WorkFlow ‘
set @sqlSelect=‘select k.* from (select ROW_NUMBER() over(order by ID) AS num,* FROM dbo.NanFang_WorkFlow ) as k where k.num>‘+@startNum+‘ and k.num<=‘+@endNum+‘ order by k.AddTime asc ‘
end

--查询总行数:执行系统内置的存储过程
exec sp_executesql @sqlCount,N‘@rowCount int output‘,@rowCount output

exec sp_executesql @sqlSelect
END