ASP.NET: PagedDataSource
图,
代码
1,前台
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <h1>PagedDataSource对象的应用 ylb, tech 2012/6/14</h1> <asp:GridView ID="gvwList" runat="server" Width="838px"> </asp:GridView> <asp:PlaceHolder ID="phPageBar" runat="server"> <table width="850"> <tr> <td> 共找到<asp:Label ID="lblCount" runat="server" Text="100"></asp:Label>记录,当前第<asp:Label ID="lblCurrent" runat="server" Text="1"></asp:Label>页 </td> <td> 第<asp:TextBox ID="txtJump" runat="server" Width="20" Text="1"></asp:TextBox> <asp:RangeValidator ID="rvTxtJump" runat="server" ErrorMessage="页码有误" ForeColor="Red" ControlToValidate="txtJump" Display="Dynamic" MinimumValue="1" Type="Integer"></asp:RangeValidator> /共<asp:Label ID="lblPageSum" runat="server" Text="9"></asp:Label>页 <asp:Button ID="btnJump" runat="server" Text="跳转" OnClick="btnJump_Click" /> <asp:Button ID="btnFirstPage" runat="server" Text="首页" OnClick="btnFirstPage_Click" /> <asp:Button ID="btnCurrentPage" runat="server" Text="上一页" OnClick="btnCurrentPage_Click" /> <asp:Button ID="btnNextPage" runat="server" Text="下一页" OnClick="btnNextPage_Click" /> <asp:Button ID="btnEndPage" runat="server" Text="尾页" OnClick="btnEndPage_Click" /> <asp:DropDownList ID="dropPageSize" runat="server" Width="40" AutoPostBack="True" OnSelectedIndexChanged="dropPageSize_SelectedIndexChanged"> <asp:ListItem>20</asp:ListItem> <asp:ListItem>50</asp:ListItem> </asp:DropDownList> </td> </tr> <tr> <td>   </td> </tr> </table> </asp:PlaceHolder> </div> </form> </body> </html>
2,后台
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Default3 : System.Web.UI.Page { private void BindShow(int currentpage,int pagesize) { //分页对象 PagedDataSource pd = new PagedDataSource(); pd.DataSource = TaoTaoOper.GetAll(); //1,把集合对象写这儿 #region 分页对象的配置 lblCount.Text = pd.Count.ToString(); //总记录数 pd.AllowPaging = true; pd.PageSize = pagesize; //分页大小 lblPageSum.Text = pd.PageCount.ToString(); //总页数 pd.CurrentPageIndex = currentpage - 1; //设置页码索引 txtJump.Text = currentpage.ToString(); //当前页 lblCurrent.Text = currentpage.ToString(); //当前页码 #endregion #region 传统分页 //首页 if (currentpage > 1) btnFirstPage.Enabled = true; else btnFirstPage.Enabled = false; //上一页 if (currentpage > 1) btnCurrentPage.Enabled = true; else btnCurrentPage.Enabled = false; //下一页 if (currentpage < pd.PageCount) btnNextPage.Enabled = true; else btnNextPage.Enabled = false; //尾页 if (currentpage < pd.PageCount) btnEndPage.Enabled = true; else btnEndPage.Enabled = false; #endregion rvTxtJump.MaximumValue = pd.PageCount.ToString(); //跳转页码验证 //2,GridView控件邦定 gvwList.DataSource = pd; gvwList.DataBind(); } #region 分页相关事件 protected void dropPageSize_SelectedIndexChanged(object sender, EventArgs e) { int pagesize = Convert.ToInt32(dropPageSize.SelectedValue); //分页大小 int currentpage = 1; //当前页码 重新加载分页邦定 //调用方法 BindShow(currentpage, pagesize); } protected void btnJump_Click(object sender, EventArgs e) { //跳转页码 int currentpage =Convert.ToInt32( txtJump.Text); int pagesize = Convert.ToInt32(dropPageSize.SelectedValue); //分页大小 //分页大小改变 BindShow(currentpage, pagesize); } protected void btnFirstPage_Click(object sender, EventArgs e) { int pagesize = Convert.ToInt32(dropPageSize.SelectedValue); //分页大小 //调用方法 BindShow(1, pagesize); } protected void btnCurrentPage_Click(object sender, EventArgs e) { int pagesize = Convert.ToInt32(dropPageSize.SelectedValue); //分页大小 int currentpage = Convert.ToInt32(txtJump.Text.Trim())-1; //调用方法 BindShow(currentpage, pagesize); } protected void btnNextPage_Click(object sender, EventArgs e) { int pagesize = Convert.ToInt32(dropPageSize.SelectedValue); //分页大小 int currentpage = Convert.ToInt32(txtJump.Text.Trim()) + 1; //调用方法 BindShow(currentpage, pagesize); } protected void btnEndPage_Click(object sender, EventArgs e) { int pagesize = Convert.ToInt32(dropPageSize.SelectedValue); //分页大小 int pageCount = Convert.ToInt32(lblPageSum.Text); //调用方法 BindShow(pageCount, pagesize); } #endregion protected void Page_Load(object sender, EventArgs e) { #region 分页,Page_Load加载调用 if (!IsPostBack) { int pagesize = Convert.ToInt32(dropPageSize.SelectedValue); //分页大小 int currentpage = 1;//pd.CurrentPageIndex + 1; //当前页码 //调用方法 BindShow(currentpage, pagesize); } #endregion } }