我的学习笔记,关于分页

把数据库绑定在repeater上
Repeater.DataSource=bll.getPageData(当前页,页数,)
Repeater.bind();

分页
1.首先获取数据的总记录数
 public int GetRecordCount(int categoryId)
        {
            string sql = "select count(*) from Books";
            if (categoryId!=0)//编号为零查看所有的书
            {
                sql = sql + " where categoryId=@categoryId";
            }
            return Convert.ToInt32(DbHelperSQL.GetSingle(sql,new SqlParameter("@categoryId",categoryId)));
        }
2.根据总的记录数在bll层算出总的页数
public int GetPageCount(int categoryId, int pageSize)
        {
            DAL.BookServices dal = new BookServices();
            int recordCount = dal.GetRecordCount(categoryId);
            return Convert.ToInt32(Math.Ceiling((double)recordCount / pageSize));
        }
3.获得分页的数据(在dal层用分页的公式)(select * from (select ROW_NUMBER() over(order by id)as num, * from Books {0})as t where t.num>=@start and t.num<=@end)
 
public DataSet GetPageData(int start, int end, int categoryId)
        {
            string sql = "select * from (select ROW_NUMBER() over(order by id)as num, * from Books {0})as t where t.num>=@start and t.num<=@end";
            sql =string.Format(sql,categoryId!=0?" where categoryId=@categoryId":"");
            SqlParameter[] parameters ={
                                           new SqlParameter("@start",start),
                                           new SqlParameter("@end",end),
                                           new SqlParameter("@categoryId",categoryId),
                                      };
            return DbHelperSQL.Query(sql,parameters);
        }
4在bll层传入开始的页数和结束的页数,计算出开始的记录数和结束的记录数据返回一个model
 public List<Model.Book> GetPageData(int pageIndex, int pageSize, int categoryId)
        {
            int start = (pageIndex - 1) * pageSize + 1;//起始位置   
            int end = pageIndex * pageSize;//结束位置
            DAL.BookServices dal = new BookServices();
            DataSet ds = dal.GetPageData(start, end, categoryId);
            return DataTableToList(ds.Tables[0]);
        }
5在UI层,传入一个当前页码(通过下一页和上一页进行控制pageindex,和一个页码的容量)把这些数据绑定到Repeater上。
this.BookListRepeater.DataSource = bll.GetPageData(pageIndex, 10, categoryId);
 this.BookListRepeater.DataBind();

在IU层注意的事项,1把ViewSate关闭EnableViewState="false"  可以防止网页的臃肿,或者直接不用asp.net的控件,,自己写html的空间进行submit的提交
                             2需要手动写一个隐藏域,把当前的页码书存储起来返回给服务器。

原文地址:https://www.cnblogs.com/shinelhui/p/2873237.html