分页原理
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="paging.aspx.cs" Inherits="paging" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <style> table { width: 500px; border: 1px solid #ccc; border-collapse: collapse; margin: 0 auto; } table td { border: 1px solid #ccc; padding: 5px; } #div1{ width:500px; margin:0 auto; } </style> </head> <body> <form id="form1" runat="server"> <div> <asp:Repeater ID="Repeater1" runat="server"> <HeaderTemplate> <table> </HeaderTemplate> <ItemTemplate> <tr> <td><%# Eval("sno")%></td> <td><%# Eval("sname")%></td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> <div id="div1"> 当前第<asp:Label ID="lbl当前页" runat="server" Text="Label">1</asp:Label>页 共<asp:Label ID="lbl总页数" runat="server" Text="Label"></asp:Label>页 每页<asp:Label ID="lbl每页数" runat="server" Text="Label">15</asp:Label>条 <asp:Button ID="btnFrist" runat="server" Text="首页" OnClick="btnFrist_Click" /> <asp:Button ID="btnUp" runat="server" Text="上页" OnClick="btnUp_Click" style="height: 21px" /> <asp:Button ID="btnDown" runat="server" Text="下页" OnClick="btnDown_Click" /> <asp:Button ID="btnLast" runat="server" Text="尾页" OnClick="btnLast_Click" /> </div> </div> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; public partial class paging : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string sql2 = "select top 15 * from students "; DataTable dt = SQLHelper.ExecuteDataTable(sql2); Repeater1.DataSource = dt; Repeater1.DataBind(); BindData(); } } private void BindData() { int pageSize = Convert.ToInt32(lbl每页数.Text); int totalRecord = Convert.ToInt32(SQLHelper.ExecuteScalar("select count(*) from students")); int currentPage = Convert.ToInt32(lbl当前页.Text); lbl总页数.Text = Convert.ToString((totalRecord + pageSize - 1) / pageSize); if (currentPage == 1) { string sql = "select top " + pageSize + " * from students"; DataTable dt = SQLHelper.ExecuteDataTable(sql); Repeater1.DataSource = dt; Repeater1.DataBind(); } else { string t = (currentPage * pageSize - pageSize).ToString(); string sql1 = "select * from students order by sno offset " + t + "rows fetch next " + pageSize + " rows only"; DataTable dt = SQLHelper.ExecuteDataTable(sql1); Repeater1.DataSource = dt; Repeater1.DataBind(); } btnFrist.Enabled = true; btnUp.Enabled = true; btnDown.Enabled = true; btnLast.Enabled = true; if (currentPage == 1) { btnFrist.Enabled = false; btnUp.Enabled = false; } else if (currentPage == Convert.ToInt32(lbl总页数.Text)) { btnLast.Enabled = false; btnDown.Enabled = false; } } protected void btnFrist_Click(object sender, EventArgs e) { lbl当前页.Text = "1"; BindData(); } protected void btnUp_Click(object sender, EventArgs e) { lbl当前页.Text = Convert.ToString(Convert.ToInt32(lbl当前页.Text) - 1); BindData(); } protected void btnDown_Click(object sender, EventArgs e) { lbl当前页.Text = Convert.ToString(Convert.ToInt32(lbl当前页.Text) + 1); BindData(); } protected void btnLast_Click(object sender, EventArgs e) { lbl当前页.Text = Convert.ToString(Convert.ToInt32(lbl总页数.Text)); BindData(); } }