前台页面 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="LinqPage.aspx.cs" Inherits="_Default" %> <!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"> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> <ContentTemplate> <asp:Repeater ID="Repeater1" runat="server"> <ItemTemplate> <%# Eval("Id") %>-- <%# Eval("LoginId")%>-- <%# Eval("LoginPwd")%>-- <%# Eval("Name")%>-- <%# Eval("Address")%>-- <%# Eval("Phone")%>-- <%# Eval("Mail")%>-- <%# Eval("UserRoles.Name")%>-- <%# Eval("UserStates.Name")%>-- <h2> </h2> </ItemTemplate> </asp:Repeater> <asp:LinkButton ID="lbFirst" runat="server" οnclick="lbFirst_Click">第一页</asp:LinkButton> <asp:LinkButton ID="lbPrevious" runat="server" οnclick="lbPrevious_Click">上一页</asp:LinkButton> <asp:LinkButton ID="lbNext" runat="server" οnclick="lbNext_Click">下一页</asp:LinkButton> <asp:LinkButton ID="lbLast" runat="server" οnclick="lbLast_Click">最后一页</asp:LinkButton> <asp:Literal ID="CountPage" runat="server"></asp:Literal> </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID="lbFirst" EventName="Click" /> <asp:AsyncPostBackTrigger ControlID="lbLast" EventName="Click" /> <asp:AsyncPostBackTrigger ControlID="lbNext" EventName="Click" /> <asp:AsyncPostBackTrigger ControlID="lbPrevious" EventName="Click" /> </Triggers> </asp:UpdatePanel> </form> </body> </html> 后台代码 public partial class _Default : System.Web.UI.Page { public int PageIndex { get { return Convert.ToInt32(ViewState["PageIndex"].ToString()); } set { ViewState["PageIndex"] = value; } } public int coundIndex { get { return Convert.ToInt32(ViewState["coundIndex"].ToString()); } set { ViewState["coundIndex"] = value; } } public int PageSize =8; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { PageIndex = 0; lbPrevious.Enabled = false; lbFirst.Enabled = false; coundIndex = (BooksCount() / PageSize) + GetPage(PageSize); BindBook(PageIndex); CountPage.Text = "1/" + coundIndex; } } DataClassesDataContext db = new DataClassesDataContext(); private int GetPage(int pageSize) { if (BooksCount() % PageSize > 0) { return 1; } return 0; } protected void BindBook(int PageIndex) { DataClassesDataContext db = new DataClassesDataContext(); var list = (from p in db.Users select p).Skip(PageIndex*PageSize).Take(PageSize); Repeater1.DataSource = list; Repeater1.DataBind(); } private int BooksCount() { int count = (from p in db.Users select p).Count(); return count; } protected void lbFirst_Click(object sender, EventArgs e) { PageIndex = 0; lbFirst.Enabled = false; lbPrevious.Enabled = false; lbNext.Enabled = true; lbLast.Enabled = true; BindBook(PageIndex); CountPage.Text = "1/" + coundIndex; } protected void lbPrevious_Click(object sender, EventArgs e) { CountPage.Text = PageIndex + "/" + coundIndex; if (PageIndex - 1 == 0) { lbPrevious.Enabled = false; lbFirst.Enabled = false; } lbNext.Enabled = true; lbLast.Enabled = true; PageIndex = PageIndex - 1; BindBook(PageIndex); } protected void lbNext_Click(object sender, EventArgs e) { if (PageIndex + 1 == coundIndex - 1) { lbNext.Enabled = false; lbLast.Enabled = false; } lbFirst.Enabled = true; lbPrevious.Enabled = true; PageIndex = PageIndex + 1; BindBook(PageIndex); CountPage.Text = (PageIndex + 1) + "/" + coundIndex; } protected void lbLast_Click(object sender, EventArgs e) { PageIndex = 0; PageIndex = coundIndex - 1; lbNext.Enabled = false; lbLast.Enabled = false; lbFirst.Enabled = true; lbPrevious.Enabled = true; BindBook(PageIndex); CountPage.Text = coundIndex + "/" + coundIndex; } }