.net分页
原文地址http://www.jb51.net/article/34754.htm
HTML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="pageview._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"> <div> <table> <tr><td> <asp:TextBox ID="txtKey" runat="server"></asp:TextBox> <asp:ImageButton ID="btnQuery" runat="server" onclick="btnQuery_Click" ImageUrl="~/images/0.jpg" Width="20" Height="20" /> <asp:Label ID="Label1" runat="server" Text=""></asp:Label> </td> </tr> <tr><td><div id="divResult" runat="server"> <asp:Repeater ID="Repeater1" runat="server"> <ItemTemplate> <table width="500"> <tr> <td><%# Eval("c001") %></td> <td><%# Eval("c002") %></td> <td><%# Eval("c003") %></td> <td><%# Eval("c004") %></td> <td><%# Eval("c005") %></td> </tr> </table> </ItemTemplate> </asp:Repeater> </div></td></tr> <tr><td> <asp:LinkButton ID="btnFirst" runat="server" onclick="btnFirst_Click">第一页</asp:LinkButton> <asp:LinkButton ID="btnBefore" runat="server" onclick="btnBefore_Click">上一页</asp:LinkButton> <asp:LinkButton ID="btnNext" runat="server" onclick="btnNext_Click">下一页</asp:LinkButton> <asp:LinkButton ID="btnLast" runat="server" onclick="btnLast_Click">最后一页</asp:LinkButton> 当前页:<%= ViewState["pageindex"] %> 总页:<%= ViewState["pagelastindex"] %></td> </tr> </table> </div> </form> </body> </html>
后台代码
using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using CnxinwaDll; namespace pageview { public partial class _Default : System.Web.UI.Page { int pagesize = 20; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ViewState["pageindex"] = 1; bind(); Count(); } } protected void bind() { int pageindex = Convert.ToInt32(ViewState["pageindex"]); Data.sqlstr = "select top " + pagesize + " * from cnxinwa002 where c001 NOT IN(SELECT TOP ((" + pageindex + "-1)* " + pagesize + ") c001 FROM cnxinwa002)"; // Response.Write(Data.sqlstr); Repeater1.DataSource = Data.GetDataSet(Data.sqlstr).Tables[0]; Repeater1.DataBind(); } //统计页数计算以后一页页码 protected void Count() { Data.sqlstr = "select count(*) from cnxinwa002"; int totalcount = Convert.ToInt32(Data.GetDataSet(Data.sqlstr).Tables[0].Rows[0][0].ToString()); //Response.Write("记录总数:" + totalcount); //Response.Write("totalcount % pagesize:" + totalcount % pagesize); if (totalcount % pagesize == 0) { ViewState["pagelastindex"] = (totalcount % pagesize); } else { ViewState["pagelastindex"] = (totalcount / pagesize) + 1; } } //搜索 protected void btnQuery_Click(object sender, ImageClickEventArgs e) { } //第一页 protected void btnFirst_Click(object sender, EventArgs e) { ViewState["pageindex"] = 1; bind(); } //上一页 protected void btnBefore_Click(object sender, EventArgs e) { int pageindex = Convert.ToInt32(ViewState["pageindex"]); if (pageindex > 1) { pageindex--; ViewState["pageindex"] = pageindex; bind(); } } //下一页 protected void btnNext_Click(object sender, EventArgs e) { int pageindex = Convert.ToInt32(ViewState["pageindex"]); if (pageindex < Convert.ToInt32(ViewState["pagelastindex"])) { pageindex++;//下一页页码自动+1 ViewState["pageindex"] = pageindex; bind(); } } //最后一页 protected void btnLast_Click(object sender, EventArgs e) { ViewState["pageindex"] = ViewState["pagelastindex"]; bind(); } } }