標籤:分頁控制項 blog os io 使用 ar 資料 art div
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(); }
使用List集合做資料來源,並且使用AspNetPager來分頁