使用List集合做数据源,并且使用AspNetPager来分页

AspNetPager 控件使用时,第一步就要在

if (!IsPostBack)
        {
              AspNetPager1.RecordCount =数据源记录总数;

            //bindData(); //使用url分页,只需在分页事件处理程序中绑定数据即可,无需在Page_Load中绑定,否则会导致数据被绑定两次
        }

第二步就是在 绑定数据源时,指定数据源中开始记录的索引与结束记录的索引,这样就可使用了

void bindData()

{

  Repeater1.DataSource = SqlHelper.ExecuteReader(CommandType.StoredProcedure, ConfigurationManager.AppSettings["pagedSPName"],
            new SqlParameter("@startIndex", AspNetPager1.StartRecordIndex),
            new SqlParameter("@endIndex", AspNetPager1.EndRecordIndex));
        Repeater1.DataBind();

}

问题:如果是用集合来做数据源,怎么办?处理办法利用PagedDataSource做数据源

假设下面例子getdata()返回的是泛型list

 void bind()
    {
       this.AspNetPager1.PageSize = 5;//分页控件每页显示记录数
       this.AspNetPager1.RecordCount = getdata().Count;//分页控件要显示数据源的记录总数
      
        PagedDataSource pds = new PagedDataSource ();//数据源分页
        pds.DataSource = getdata();//得到数据源
        pds.AllowPaging = true;//允许分页
        pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;//当前分页数据源的页面索引
        pds.PageSize = AspNetPager1.PageSize;//分页数据源的每页记录数
        this.Repeater1.DataSource = pds;//指定为控件数据源
        this.Repeater1.DataBind();
     
           }
原文地址:https://www.cnblogs.com/hausthy/p/3958191.html