Reapter使用table绑定数据,使用AspNetPager进行分页

时间:2022-09-13 17:30:26

1.UI展示

<div>
    <table>
        <asp:Repeater ID="Repeater1" runat="server"
            onitemdatabound="Repeater1_ItemDataBound">
        <HeaderTemplate>
       
        <thead>
        <th>
        FirstName
        </th>
        <th>
        LastName
        </th>
        <th>
        Title
        </th>
        <th>
        BirthDate
        </th>
        <th>
        Address
        </th>
        </thead>
        </HeaderTemplate>
        <ItemTemplate>
        <tbody>
        <tr>
        <td>
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        </td>
        <td>
            <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
        </td>
        <td>
            <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
        </td>
        <td>
            <asp:Label ID="Label4" runat="server" Text="Label"></asp:Label>
        </td>
        <td>
            <asp:Label ID="Label5" runat="server" Text="Label"></asp:Label>
        </td>
        </tr>
        </tbody>
       
        </ItemTemplate>
        </asp:Repeater>
    </table>
    <webdiyer:AspNetPager ID="AspNetPager1" runat="server" FirstPageText="首页"
             AlwaysShow="true"
            LastPageText="尾页" NextPageText="下一页" PageIndexBoxType="TextBox"
            PrevPageText="上一页" ShowBoxThreshold="5" ShowPageIndexBox="Auto"
            SubmitButtonText="Go" TextAfterPageIndexBox="页"
            TextBeforePageIndexBox="转到" onpagechanged="AspNetPager1_PageChanged"
            UrlPaging="True" >
            </webdiyer:AspNetPager>
    </div>

2.后台代码

protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                InitReapter();
            }
        }

        private void InitReapter()
        {
            NorthwindEntities db = new NorthwindEntities();
            int recordCount = db.Employees.ToList().Count;
            AspNetPager1.RecordCount = recordCount;
            AspNetPager1.PageSize = 2;
            this.Repeater1.DataSource = db.Employees.ToList().Skip((currentPage - 1)*2).Take(2);
            this.Repeater1.DataBind();
        }

        private void InitAspNetPager(int recordCount)
        {
            AspNetPager1.RecordCount = recordCount;
            AspNetPager1.PageSize = 2;
        }

        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Employees tmp = (Employees)e.Item.DataItem;
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Label Label1 = (Label)e.Item.FindControl("Label1");
                Label1.Text = tmp.FirstName;
                Label1 = (Label)e.Item.FindControl("Label2");
                Label1.Text = tmp.LastName;
                Label1 = (Label)e.Item.FindControl("Label3");
                Label1.Text = tmp.Title;
                Label1 = (Label)e.Item.FindControl("Label4");
                Label1.Text = tmp.BirthDate.ToString();
                Label1 = (Label)e.Item.FindControl("Label5");
                Label1.Text = tmp.Address;
            }
        }

        public int currentPage
        {
            get { return Convert.ToInt32(Request.QueryString["page"]) == 0 ? 1 : Convert.ToInt32(Request.QueryString["page"]); }
        }