标签:
<asp:Repeater ID="RptrReview" runat="server" OnItemCommand="RptrReview_ItemCommand" OnItemDataBound="RptrReview_ItemDataBound" > ...... </asp:Repeater> <ul class="pager"> <li class="previous"><asp:LinkButton ID="LnkBtnPrevious" runat="server" OnCommand="RepeaterPageChange_Command" CommandName="Previous" CommandArgument="-1" >← 上一页</asp:LinkButton></li> <asp:Label ID="LblPageIndex" runat="server" Text="1" style="color:#428BCA;font-size:24px;"></asp:Label> <li class="next"><asp:LinkButton ID="LnkBtnNext" runat="server" OnCommand="RepeaterPageChange_Command" CommandName="Next" CommandArgument="1">下一页 →</asp:LinkButton></li> </ul>
protected void RepeaterPageChange_Command(object sender, CommandEventArgs e) { PagedDataSource pds = new PagedDataSource(); pds.DataSource=GetReviewDV();// 将DataView绑定到PagedDataSource上 pds.AllowPaging = true; // 设置指示是否启用分页的值 pds.PageSize = 3; // 设置要在每页显示的数量 int pageIndex=Convert.ToInt32(LblPageIndex.Text.ToString())-1; pds.CurrentPageIndex = pageIndex;//获取 PagedDataSource当前页码 if (e.CommandName=="Next"&&pds.IsLastPage)//已经是末页,还向下翻页的情况,跳过 { RptrReview.DataSource = pds; RptrReview.DataBind(); } else if (e.CommandName == "Previous"&&pds.IsFirstPage)//已经是首页,还向上翻页的情况,跳过 { RptrReview.DataSource = pds; RptrReview.DataBind(); } else//通过 e.CommandArgument 修改 PagedDataSource 页码 { pageIndex = pageIndex + Convert.ToInt32(e.CommandArgument); LblPageIndex.Text = (pageIndex + 1).ToString(); pds.CurrentPageIndex = pageIndex; // 设置当前页的索引。 RptrReview.DataSource = pds; RptrReview.DataBind(); } }
/// <summary> /// 绑定RptrReview的第pageIndex页 /// </summary> /// <param name="pageIndex"></param> protected void BindRptrReview(ref int pageIndex) { PagedDataSource pds = new PagedDataSource(); pds.DataSource = GetReviewDV(); pds.AllowPaging = true; // 设置指示是否启用分页的值 pds.PageSize = 3; // 设置要在每页显示的数量 //处理pageIndex极端情况 if(pageIndex<0) { pageIndex=0; } else if(pageIndex>(pds.PageCount-1)) { pageIndex=pds.PageCount-1; } pds.CurrentPageIndex = pageIndex; // 设置当前页的索引。 RptrReview.DataSource = pds; RptrReview.DataBind(); } protected void RepeaterPageChange_Command(object sender, CommandEventArgs e) { int pageIndex = Convert.ToInt32(LblPageIndex.Text.ToString()) - 1; pageIndex = pageIndex + Convert.ToInt32(e.CommandArgument); BindRptrReview(ref pageIndex); LblPageIndex.Text = (pageIndex + 1).ToString(); }
ASP.NET WebForm 通过 PagedDataSource 实现 Repeater 的分页
标签:
原文地址:http://www.cnblogs.com/moonache/p/5438291.html