通过创建多一个Repeater 来作为分页用
<div style="overflow: hidden; padding-bottom: 20px; text-align: center;"> <asp:Repeater ID="rptPaging" runat="server" OnItemCommand="rptPaging_ItemCommand"> <ItemTemplate> <asp:LinkButton ID="btnPage" style="background:darkgray; border:solid 1px #666; font:8pt tahoma;" CommandName="Page" CommandArgument="<%# Container.DataItem %>" runat="server" ForeColor="White" Font-Bold="True"><%#Container.DataItem %> </asp:LinkButton> </ItemTemplate> </asp:Repeater> </div>
后台代码
//此属性将包含当前页码 public int PageNumber { get { if (ViewState["PageNumber"] != null) { return Convert.ToInt32(ViewState["PageNumber"]); } else { return 0; } } set { ViewState["PageNumber"] = value; } }
//绑定记录 public void BindList() { DataTable dt = new DataTable(); if (am.ReadMyBrowse(uiv.UserID, out dt) > 0) { if (dt != null && dt.Rows.Count > 0) { PagedDataSource pgitems = new PagedDataSource(); pgitems.DataSource = dt.DefaultView; pgitems.AllowPaging = true; pgitems.PageSize = 20; pgitems.CurrentPageIndex = PageNumber; if (pgitems.PageCount > 1) { rptPaging.Visible = true; ArrayList pages = new ArrayList(); for (int i = 0; i <= pgitems.PageCount - 1; i++) { pages.Add((i + 1).ToString()); } rptPaging.DataSource = pages; rptPaging.DataBind(); } else { rptPaging.Visible = false; } repeater1.DataSource = pgitems; repeater1.DataBind(); //repeater1.DataSource = dt; //repeater1.DataBind(); } else { repeater1.DataSource = ""; repeater1.DataBind(); rptPaging.DataSource = ""; rptPaging.DataBind(); } } else { repeater1.DataSource = ""; repeater1.DataBind(); rptPaging.DataSource = ""; rptPaging.DataBind(); } }
protected void rptPaging_ItemCommand(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e) { PageNumber = Convert.ToInt32(e.CommandArgument) - 1; BindRepeater(); }
效果:
时间: 2024-10-09 21:49:21