在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1.效果图2.代码说明1.翻页按钮
前台两个LinkButton(上一页、下一页),设置不同的CommandName、CommandArgument ,绑定同一个OnCommand事件,在后台OnCommand事件中分别处理
2.PagedDataSource
将原始数据转为 DataView 然后绑定到 PagedDataSource,通过 PagedDataSource 的内置属性实现分页和翻页,最后再绑定到 Repeater上面
2.Code1.front end<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>
2.code-behindprotected 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(); } }
3.兼容版/// <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(); }
3.Reference |
请发表评论