前台页面使用方法:在使用之前先注册<%@ Register Assembly='AspNetPager' Namespace='Wuqi.Webdiyer' TagPrefix='webdiyer' %>
第一种赋值方法:把当前索引页和每页显示条数传递到存储过程中,计算好需要取出的数据后再给分页控件赋值,这种方法适用于数据量大的情况 //清空列表 this.gvCompetitionItems.DataSource = null; this.gvCompetitionItems.DataBind(); //获取数据集合 int index = this.AspNetPager1.CurrentPageIndex; int size = this.AspNetPager1.PageSize; DataSet ds = bll.GetListForPaged(index, size); int pageCount = 0; if (ds != null && ds.Tables[0].Rows.Count > 0) { pageCount = Convert.ToInt32(ds.Tables[0].Rows[0]['PageCount']); //绑定到分页控件 this.AspNetPager1.RecordCount = pageCount; PagedDataSource pds = new PagedDataSource(); pds.DataSource = ds.Tables[0].DefaultView; pds.VirtualCount = this.AspNetPager1.PageSize; pds.AllowPaging = true; pds.CurrentPageIndex = this.AspNetPager1.CurrentPageIndex - 1; pds.PageSize = this.AspNetPager1.PageSize; this.gvCompetitionItems.DataSource = ds.Tables[0].DefaultView; this.gvCompetitionItems.DataBind(); } else { this.AspNetPager1.RecordCount = 0; }存储过程写法:ALTER PROCEDURE [USP_SEL_QuerySuperiorCompetition]-- Add the parameters for the stored procedure here@PageIndex int, @PageSize intASBEGIN-- SET NOCOUNT ON added to prevent extra result sets from-- interfering with SELECT statements.SET NOCOUNT ON; --计算总数 declare @PageCount int -- 'BC305CB7-73C7-4B9F-8310-BEB38E8A3C47'是字典表中上级下发(比赛组织方式)的ID select @PageCount=COUNT(P_CompetitionMessageID) from T_BUS_CompetitionMessage where F_ModeID='BC305CB7-73C7-4B9F-8310-BEB38E8A3C47' -- Insert statements for procedure here --查询分页数据select *,@PageCount as [PageCount]from( SELECT * ,ROW_NUMBER()over (order by C_CreateTime desc) as row_num ,(select C_DictName from T_SYS_Dict where P_DictID= F_ModeID) as C_ModeName from T_BUS_CompetitionMessage where F_ModeID='BC305CB7-73C7-4B9F-8310-BEB38E8A3C47') as tblwhere tbl.row_numbetween(@PageIndex-1)*@PageSize+1and@PageIndex*@PageSizeEND
第二种赋值方法:直接取出数据赋值给分页控件//获取短期培训数据集合 DataSet ds = T_BUS_TrainingPlan_BLL.GetList(); //绑定到分页控件 this.AspNetPager1.RecordCount = ds.Tables[0].Rows.Count; PagedDataSource pds = new PagedDataSource(); pds.DataSource = ds.Tables[0].DefaultView; pds.AllowPaging = true; pds.CurrentPageIndex = this.AspNetPager1.CurrentPageIndex - 1; pds.PageSize = this.AspNetPager1.PageSize; //绑定到GridView控件 this.GridView_List.DataSource = pds; this.GridView_List.DataBind();