首页 > 代码库 > 分页功能的实现

分页功能的实现

  使用repeter控件来实现功能。

  功能查看:

  前台代码的是实现如下:

 1         <asp:Repeater ID="Repeater1" runat="server"> 2             <HeaderTemplate> 3                 <table class="frmtable"> 4                     <tr onm ouseover="this.style.backgroundColor=‘#FF0000‘"  onm ouseout ="this.style.backgroundColor=‘white‘" > 5                         <td>编号</td> 6                         <td>用户名</td> 7                         <td>年龄</td> 8                         <td>班级</td> 9                         <td>登记时间</td>10                         <td>操作</td>11                     </tr>12                 </table>13             </HeaderTemplate>14             <ItemTemplate>15                 <table class="frmtable">16                     <tr onm ouseover="this.style.backgroundColor=‘#FF0000‘"  onm ouseout ="this.style.backgroundColor=‘white‘" >17                         <td><%#Eval("id") %></td>18                         <td><%#Eval("username") %></td>19                         <td><%#Eval("age") %></td>20                         <td><%#Eval("class") %></td>21                         <td>22                             <%#ToDateTime(Eval("time")) %>23                         </td>24                         <td>25                             <a href=http://www.mamicode.com/"javascript:confirmDelete(‘delete.aspx?deleteid=<%#Eval("id") %>‘)"><%#Eval("handle") %></a> 26                         </td>                        27                     </tr>28                 </table>29             </ItemTemplate>30         </asp:Repeater>31 32         <div>33             <%=PageText %>34         </div>35         <img alt ="第一张" id="imgs" src=http://www.mamicode.com/"image/1.jpg" style="z-index: 1; left: 1030px; top: 91px; position: absolute; height: 300px; width: 300px" />
Repeter前台实现代码

  后台代码如下:

 1     protected string PageText = ""; 2     protected void Page_Load(object sender, EventArgs e) 3     { 4         MySqlConnection myconn = DBhelper.GetConnection(); 5         //PageText += "asd"; 6         setPageList(); 7         myconn.Close(); 8     } 9     /// <summary>10     /// 分页显示表格代码11     /// </summary>12     public void setPageList()13     {14         double pageConfig = 10; //每页多少条15         double infoCount = 0; //共有多少条记录16         //读取数据库中的记录17         MySqlConnection conn = DBhelper.GetConnection();18         MySqlCommand cmd = new MySqlCommand("select count(*) from login", conn);//求有多少条记录19         infoCount = Convert.ToInt16(cmd.ExecuteScalar());20         double lastly = infoCount / pageConfig;// 求分页数,共有多少页21         int page = Convert.ToInt16(Request.QueryString["page"]);// 当前第几页22         if (page == 0)23         {24             page = 1;25         }26         //int first = 1;27         int last = page - 1;// 上一页28         int nextpage = page + 1;// 下一页 做翻页用29         if (lastly == 1)30         { }31         else32         {33             if (Convert.ToInt16(lastly.ToString().Split(.).Length) != 1)34             {35                 lastly = Convert.ToInt16(lastly.ToString().Split(.)[0]) + 1; //分页数如果不是整数那么加136             }37         }38         string sql = "";39         if (page == 1)40         {41             sql = "select * from login limit 0,10";42         }43         else44         {45             sql = "select * from login limit " + (page - 1) * 10 + ",10 ";46         }47         MySqlDataAdapter da = new MySqlDataAdapter(sql, conn);48         DataSet ds = new DataSet();49         da.Fill(ds);50         this.Repeater1.DataSource = ds;51         this.Repeater1.DataBind();52         if (page > 1)53         {54             PageText += "<a href=http://www.mamicode.com/"?page=" + (page > 1 ? page - 1 : 1) + "\">上一页</a>";55         }56         for (int i = 1; i <= lastly; i++)57         {58             if (page == i)59             {60                 PageText += "<b>[" + i.ToString() + "]</b>";61             }62             else63             {64                 PageText += "<a href=http://www.mamicode.com/‘?page=" + i.ToString() + "‘>" + i.ToString() + "></a>";65             }66         }67         if (page < lastly)68         {69             PageText += "<a href=http://www.mamicode.com/"?page=" + (page >= lastly ? lastly : page + 1) + "\">下一页</a>";70         }71     }
Repeter后台实现代码