页面代码
<div class="bookList"> <asp:Repeater ID="rpBooks" runat="server"> <HeaderTemplate> <ul class="title"> <li class="bName">书名</li> <li class="bAuthor">作者</li> <li class="bPrice">价格</li> </ul> </HeaderTemplate> <ItemTemplate> <ul class="data"> <li class="bName"><%#eval_r("title").ToString().Length > 20 ? eval_r("title").ToString().Substring(0, 20)+"..." : eval_r("title").ToString()%></li> <li class="bAuthor"><%#eval_r("author").ToString().Length > 20 ? eval_r("author").ToString().Substring(0, 20) + "..." : eval_r("author").ToString()%></li> <li class="bPrice"><%#eval_r("unitprice") %></li> </ul> </ItemTemplate> <AlternatingItemTemplate> <ul class="data" style="background-color:Gray;"> <li class="bName"><%#eval_r("title").ToString().Length > 20 ? eval_r("title").ToString().Substring(0, 20)+"..." : eval_r("title").ToString()%></li> <li class="bAuthor"><%#eval_r("author").ToString().Length > 20 ? eval_r("author").ToString().Substring(0, 20) + "..." : eval_r("author").ToString()%></li> <li class="bPrice"><%#eval_r("unitprice") %></li> </ul> </AlternatingItemTemplate> </asp:Repeater> <div style="clear:both;"></div> <!--分页跳转--> <p class="page"> <asp:LinkButton ID="lbtnFirst" CommandName="first" runat="server" oncommand="lbtnFirst_Command">首页</asp:LinkButton> <asp:LinkButton ID="lbtnPrevious" CommandName="previous" runat="server" oncommand="lbtnFirst_Command">上一页</asp:LinkButton> <asp:Label ID="lblPageIndex" runat="server" Text="1"></asp:Label> <asp:Label ID="Label3" runat="server" Text="/"></asp:Label> <asp:Label ID="lblPageSum" runat="server" Text=""></asp:Label> <asp:LinkButton ID="lbtnNext" CommandName="next" runat="server" oncommand="lbtnFirst_Command">下一页</asp:LinkButton> <asp:LinkButton ID="lbtnLast" CommandName="last" runat="server" oncommand="lbtnFirst_Command">尾页</asp:LinkButton> 跳转到 <asp:TextBox ID="txtPageIndex" runat="server" Width="30px"></asp:TextBox> <asp:Button ID="btnGo" runat="server" Text="跳转到" CommandName="btn" oncommand="lbtnFirst_Command" /> 共<asp:Label ID="lblCount" runat="server" Text="Label"></asp:Label> 条记录</p> </div>
后置代码
int index =1;//初始化当前页索引,默认显示第一页 int pageSize = 5;//设置每页显示条数 Common.Page page =null; protected void Page_Load(object sender, EventArgs e) { //前段代码中给当前页标签默认为第一页,用标签保存当前页面索引 index =int.Parse(lblPageIndex.Text); //在页面首次加载的时候,加载总页数,总记录数和信息列表 if (!IsPostBack ) { page = new Common.Page(index, pageSize); this.lblCount.Text = page.Count.ToString(); this.lblPageSum.Text = page.PageSum.ToString(); BookBind(); } } //翻页按钮命令事件 protected void lbtnFirst_Command(object sender, CommandEventArgs e) { page = new Common.Page(index, pageSize); string commandName = e.CommandName; switch (commandName) { case "first": page.PageIndex = 1; break; case "previous": if (index==1) { page.PageIndex = 1; } else { page.PageIndex =index-1; } break; case "next": if (index == page.PageSum) { page.PageIndex = page.PageSum; } else { page.PageIndex=index+1; } break; case "last": page.PageIndex =page.PageSum; break; case "btn": page.PageIndex = int.Parse(txtPageIndex.Text); break; } this.lblPageIndex.Text = page.PageIndex.ToString(); BookBind(); } //给Repeater控件绑定数据 private void BookBind() { this.rpBooks.DataSource = page.CurPager; this.rpBooks.DataBind(); } //获取记录总数的方法 public int GetCount() { return page.Count; } //获取总页数的方法 public int GetPageSum() { return page.PageSum; }
注意事项
本例使用label标签保存当前页面索引,也可以使用视图状态保存。