Repeater 自定义分页

来源:互联网 发布:周立功嵌入式linux 编辑:程序博客网 时间:2024/05/16 07:02
HTML代码:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" >
  4. <head runat="server">
  5.     <title>无标题页</title>
  6. </head>
  7. <body>
  8.     <form id="form1" runat="server">
  9.     <div>
  10.         <div style="text-align: center">
  11.             <table border="0" cellpadding="0" cellspacing="0" style="z-index: 100; left: 115px;
  12.                 width: 726px; position: absolute; top: 23px; height: 122px">
  13.                 <tr>
  14.                     <td style="width: 118px; height: 3px">
  15.                              
  16.                         <asp:Repeater ID="Repeater1" runat="server">
  17.                         <ItemTemplate>
  18.                         <table width=400px height=100px>
  19.                           <tr>
  20.                           <td>员工编号:<%#DataBinder.Eval(Container.DataItem,"job_id")%></td>
  21.                           <td>员工描述:<%#Eval("job_desc")%></td>
  22.                           </tr>
  23.                         </table>
  24.                         </ItemTemplate>
  25.                         <HeaderTemplate><h3>员工信息</h3></HeaderTemplate>
  26.                         <FooterTemplate><h3>员工列表</h3></FooterTemplate>
  27.                         <SeparatorTemplate>
  28.                         <hr color="red" />
  29.                         </SeparatorTemplate>
  30.                         <AlternatingItemTemplate>
  31.                         <table width=400px height=100px>
  32.                           <tr>
  33.                           <td><font color='red'>员工编号:<%#DataBinder.Eval(Container.DataItem,"job_id")%></font></td>
  34.                           <td><font color='red'>员工描述:<%#Eval("job_desc")%></font></td>
  35.                           </tr>
  36.                         </table>
  37.                         </AlternatingItemTemplate>
  38.                         </asp:Repeater>
  39.                     </td>
  40.                 </tr>
  41.                 <tr>
  42.                     <td style="width: 118px; height: 19px;">
  43.                         <asp:Label ID="Label1" runat="server" Text="总页码:"></asp:Label>
  44.                         <asp:Label ID="lblCount" runat="server"></asp:Label>
  45.                                      <asp:Label ID="Label3" runat="server"
  46.                             Text="当前页码:"></asp:Label>
  47.                         <asp:Label ID="lblCurrentPage" runat="server"></asp:Label>
  48.                                          <asp:LinkButton ID="lnkPrev"
  49.                             runat="server" OnClick="lnkPrev_Click">上一页</asp:LinkButton>
  50.                                         
  51.                         <asp:LinkButton ID="lnkNext" runat="server" OnClick="lnkNext_Click">下一页</asp:LinkButton></td>
  52.                 </tr>
  53.             </table>
  54.         </div>
  55.     
  56.     </div>
  57.     </form>
  58. </body>
  59. </html>
  60. 后台代码:
    1. using System;
    2. using System.Data;
    3. using System.Configuration;
    4. using System.Collections;
    5. using System.Web;
    6. using System.Web.Security;
    7. using System.Web.UI;
    8. using System.Web.UI.WebControls;
    9. using System.Web.UI.WebControls.WebParts;
    10. using System.Web.UI.HtmlControls;
    11. using System.Data.SqlClient;
    12. public partial class Default3 : System.Web.UI.Page
    13. {
    14.     /// <summary>
    15.     /// Post方式分页
    16.     /// </summary>
    17.     /// <param name="sender"></param>
    18.     /// <param name="e"></param>
    19.     protected void Page_Load(object sender, EventArgs e)
    20.     {
    21.         if (!Page.IsPostBack)
    22.         {
    23.             this.lblCurrentPage.Text = "1";
    24.             this.DataBindToRepeater();
    25.         }
    26.     }
    27.     public void DataBindToRepeater()
    28.     {
    29.         SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=;database=pubs");
    30.         conn.Open();
    31.         SqlCommand cmd = new SqlCommand("select * from jobs", conn);
    32.         SqlDataAdapter sda = new SqlDataAdapter();
    33.         sda.SelectCommand = cmd;
    34.         DataSet ds = new DataSet();
    35.         sda.Fill(ds, "jobs");
    36.         //设置分页数据源,实际上它是DataGird控件的封装类
    37.         PagedDataSource pds = new PagedDataSource();
    38.         pds.DataSource = ds.Tables["jobs"].DefaultView;
    39.         //允许分页
    40.         pds.AllowPaging = true;
    41.         //每页显示记录条数
    42.         pds.PageSize = 3;
    43.         //当前页码数
    44.         int curPage = Convert.ToInt32(this.lblCurrentPage.Text);
    45.         //索引号分页
    46.         pds.CurrentPageIndex = curPage - 1;
    47.         //总页码数
    48.         this.lblCount.Text = Convert.ToString(pds.PageCount);
    49.         //显示分页控件的状态
    50.         this.lnkPrev.Enabled = true;
    51.         this.lnkNext.Enabled = true;
    52.         //判断是第一页
    53.         if (pds.IsFirstPage)
    54.         {
    55.             this.lnkPrev.Enabled = false;
    56.         }
    57.         //判断是最后页
    58.         if (pds.IsLastPage)
    59.         {
    60.             this.lnkNext.Enabled = false;
    61.         }
    62.         this.Repeater1.DataSource = pds;
    63.         this.Repeater1.DataBind();
    64.     }
    65.     protected void lnkPrev_Click(object sender, EventArgs e)
    66.     {
    67.         this.lblCurrentPage.Text = Convert.ToString(Convert.ToInt32(this.lblCurrentPage.Text) - 1);
    68.         DataBindToRepeater();
    69.     }
    70.     protected void lnkNext_Click(object sender, EventArgs e)
    71.     {
    72.         this.lblCurrentPage.Text = Convert.ToString(Convert.ToInt32(this.lblCurrentPage.Text) + 1);
    73.         DataBindToRepeater();
    74.     }
    75. }
原创粉丝点击