创建测试数据库:TEMP :
1 CREATE DATABASE TEMP
创建测试表:Person:
1 CREATE TABLE Person 2 ( 3 PID INT IDENTITY(1, 1), 4 PName VARCHAR(20), 5 PAge INT, 6 PSex VARCHAR(10), 7 PJob VARCHAR(50) 8 )
插入数据:
1 INSERT INTO Person (PName, PAge, PSex, PJob) VALUES ('张三', 18, '男', '软件工程师') 2 INSERT INTO Person (PName, PAge, PSex, PJob) VALUES ('张三', 18, '男', '软件工程师') 3 INSERT INTO Person (PName, PAge, PSex, PJob) VALUES ('张三', 18, '男', '软件工程师') 4 · 5 · 6 · 7 · 8 · 9 ·
最终的表:
以上是数据库的操作,下面是代码的编写。
首先是前台代码:
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Debug ="true"%> 2 3 <!DOCTYPE html> 4 5 <html xmlns="http://www.w3.org/1999/xhtml"> 6 <head runat="server"> 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 8 <title></title> 9 </head> 10 <body> 11 <form id="form1" runat="server"> 12 <div> 13 <h1>Repeater分页</h1> 14 <asp:Repeater ID="Repeater1" runat="server" > 15 16 <HeaderTemplate> 17 <table><tr><th>编号</th><th>姓名</th><th>年龄</th><th>性别</th><th>工作</th></tr> 18 </HeaderTemplate> 19 <ItemTemplate> 20 <tr> 21 <td><asp:Label ID="Label5" runat="server" Text='<%#Eval("PID") %>'></asp:Label></td> 22 <td><asp:Label ID="Label1" runat="server" Text='<%#Eval("PName") %>'></asp:Label></td> 23 <td><asp:Label ID="Label2" runat="server" Text='<%#Eval("PAge") %>'></asp:Label></td> 24 <td><asp:Label ID="Label3" runat="server" Text='<%#Eval("PSex") %>'></asp:Label></td> 25 <td><asp:Label ID="Label4" runat="server" Text='<%#Eval("PJob") %>'></asp:Label></td> 26 </tr> 27 </ItemTemplate> 28 <FooterTemplate> 29 </table> 30 </FooterTemplate> 31 </asp:Repeater> 32 <div id="fenye"> 33 <asp:Label ID="lbNow" runat="server" Text="当前页"></asp:Label> 34 <asp:Label ID="lbPage" runat="server" Text="1"></asp:Label> 35 <asp:Label ID="lbAll" runat="server" Text="总页数"></asp:Label> 36 <asp:Label ID="lbCount" runat="server" Text=""></asp:Label> 37 <asp:LinkButton ID="lbtnFirst" runat="server" onclick ="lbtnFirst_Click">首页</asp:LinkButton> 38 <asp:LinkButton ID="lbtnUp" runat="server" onclick ="lbtnUp_Click">上一页</asp:LinkButton> 39 <asp:LinkButton ID="lbtnDown" runat="server" onclick ="lbtnDown_Click">下一页</asp:LinkButton> 40 <asp:LinkButton ID="lbtnLast" runat="server" onclick ="lbtnLast_Click">尾页</asp:LinkButton> 41 <asp:DropDownList ID="DropDownList1" runat="server" Width="80px"> 42 </asp:DropDownList> 43 <asp:LinkButton ID="lbtnGo" runat="server" BackColor="LightBlue" 44 BorderWidth="2px" BorderColor="Blue" onclick ="lbtnGo_Click" style="width: 20px">Go</asp:LinkButton> 45 </div> 46 <br /> 47 </div> 48 49 </form> 50 </body> 51 </html>
web.config文件的连接字符串:
1 <connectionStrings> 2 <add name="LinqTestConnectionString" connectionString="data source = (local); database = TEMP; integrated security = true"/> 3 </connectionStrings>
后台代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 8 using System.Data; 9 using System.Data.SqlClient; 10 using System.Configuration; 11 12 public partial class _Default : System.Web.UI.Page 13 { 14 SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["LinqTestConnectionString"].ToString()); 15 16 public SqlCommand sqlCmd = null; 17 18 int countPage = 20;//每一页有多少条数据 19 20 protected void Page_Load(object sender, EventArgs e) 21 { 22 if (!IsPostBack) 23 { 24 DropListBind();//为DropDownList赋值 25 Show();//初始化显示第一页,默认当前为第一页 26 State();//初始化导航按钮的使用状态 27 } 28 } 29 30 /// <summary> 31 /// 第一页 32 /// </summary> 33 /// <param name="sender"></param> 34 /// <param name="e"></param> 35 protected void lbtnFirst_Click(object sender, EventArgs e) 36 { 37 lbPage.Text = "1"; 38 Show(); 39 State(); 40 } 41 42 /// <summary> 43 /// 上一页 44 /// </summary> 45 /// <param name="sender"></param> 46 /// <param name="e"></param> 47 protected void lbtnUp_Click(object sender, EventArgs e) 48 { 49 lbPage.Text = (Convert.ToInt32(lbPage.Text) - 1).ToString(); 50 Show(); 51 State(); 52 } 53 54 /// <summary> 55 /// 下一页 56 /// </summary> 57 /// <param name="sender"></param> 58 /// <param name="e"></param> 59 protected void lbtnDown_Click(object sender, EventArgs e) 60 { 61 lbPage.Text = (Convert.ToInt32(lbPage.Text) + 1).ToString(); 62 Show(); 63 State(); 64 } 65 66 /// <summary> 67 /// 最后一页 68 /// </summary> 69 /// <param name="sender"></param> 70 /// <param name="e"></param> 71 protected void lbtnLast_Click(object sender, EventArgs e) 72 { 73 lbPage.Text = lbCount.Text; 74 Show(); 75 State(); 76 } 77 78 /// <summary> 79 /// GO按钮 80 /// </summary> 81 /// <param name="sender"></param> 82 /// <param name="e"></param> 83 protected void lbtnGo_Click(object sender, EventArgs e) 84 { 85 lbPage.Text = DropDownList1.SelectedValue; 86 Show(); 87 State(); 88 } 89 90 //绑定DropDwonList控件 91 public void DropListBind() 92 { 93 sqlConn.Open(); 94 95 sqlCmd = new SqlCommand("select count(*) from Person", sqlConn);//获取数据库中信息的总条数 96 97 int page = Convert.ToInt32(sqlCmd.ExecuteScalar()); 98 99 //每页显示countPage条,算出总页数,并为DropDownList赋值 100 101 //使用ceiling(天花板函数)--MSDN示例 102 /* 103 * double[] values = {7.03, 7.64, 0.12, -0.12, -7.1, -7.6}; 104 * Console.WriteLine(" Value Ceiling Floor\n"); 105 * foreach (double value in values) 106 * { 107 * Console.WriteLine("{0,7} {1,16} {2,14}", value, Math.Ceiling(value), Math.Floor(value)); 108 * } 109 * The example displays the following output to the console: 110 * Value Ceiling Floor 111 * 7.03 8 7 112 * 7.64 8 7 113 * 0.12 1 0 114 * -0.12 0 -1 115 * -7.1 -7 -8 116 * -7.6 -7 -8 117 */ 118 //为什么Page要乘以1.0,因为如果不乘的话,就会默认转换成整型,得不到小数,就无法使用ceiling方法了。 119 this.lbCount.Text = (Math.Ceiling(((page * 1.0/countPage)))).ToString(); 120 121 int[] num = new int[Convert.ToInt32(lbCount.Text)]; 122 123 for (int i = 1; i <= Convert.ToInt32(lbCount.Text); i++)//如果使用 i= 0,那么在前台显示的时候买第一个值是0. 124 { 125 num[i - 1] = i; 126 } 127 128 DropDownList1.DataSource = num; 129 130 DropDownList1.DataBind(); 131 } 132 133 /// <summary> 134 /// 状态设置 135 /// </summary> 136 public void State() 137 { 138 if (lbPage.Text == "1")//如果当前页为第一页,则前一页和首页按钮禁用 139 { 140 lbtnFirst.Enabled = false; 141 lbtnUp.Enabled = false; 142 lbtnLast.Enabled = true; 143 lbtnDown.Enabled = true; 144 } 145 if (lbPage.Text == lbCount.Text)//如果当前页为最后一页,则后一页和尾页按钮禁用 146 { 147 lbtnFirst.Enabled = true; 148 lbtnUp.Enabled = true; 149 lbtnLast.Enabled = false; 150 lbtnDown.Enabled = false; 151 } 152 if (Convert.ToInt32(lbPage.Text) > 1 && Convert.ToInt32(lbPage.Text) < Convert.ToInt32(lbCount.Text))//如果当前也在首页和尾页之间则四个按钮均可用 153 { 154 lbtnFirst.Enabled = true; 155 lbtnUp.Enabled = true; 156 lbtnLast.Enabled = true; 157 lbtnDown.Enabled = true; 158 } 159 } 160 161 /// <summary> 162 /// 显示数据,绑定数据 163 /// </summary> 164 public void Show() 165 { 166 //从数据库中筛选信息,仅加载当前请求的那一页的信息,效率会相对比较高,并非全部加载 167 string sql = "select * from Person where PID>'" + (Convert.ToInt32(lbPage.Text) - 1) * countPage + "' and PID<='" + Convert.ToInt32(lbPage.Text) * countPage + "' order by PID ASC"; 168 sqlCmd = new SqlCommand(sql, sqlConn); 169 SqlDataAdapter sAdapter = new SqlDataAdapter(sqlCmd); 170 DataSet ds = new DataSet(); 171 sAdapter.Fill(ds, "Result"); 172 sqlConn.Close(); 173 Repeater1.DataSource = ds.Tables["Result"].DefaultView; 174 Repeater1.DataBind(); 175 } 176 }
最终效果:
*所有解释,写在了代码的注释里,所以要详细的看代码。