Repeater实现分页功能

运行效果:前台代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ProjectList.aspx.cs" Inherits="LoveFundWeb.后台.ProjectList" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <link href="../Content/bootstrap.css" rel="stylesheet" />
</head>
<body>
    <form id="form1" runat="server">
           <div style="width:80%;margin:50px auto">
             <h1 style="text-align:center">项目列表</h1>
             <table class="table table-bordered table-hover">
                 <tr>
                     <th>项目编号</th>
                     <th>项目名称</th>
                     <th>需要金额</th>
                     <th>预计天数</th>
                     <th>类别ID</th>
                     <th>所在省份</th>
                     <th>所在城市</th>
                     <th>项目简介</th>
                     <th>项目详情</th>
                     <th>通过状态</th>
                     <th>操作</th>
                 </tr>
                 <asp:Repeater ID="rpProjectList" runat="server">
                     <ItemTemplate>
                         <tr>
                             <td><%# Eval("ProId") %></td>
                             <td><%# Eval("ProName") %></td>
                             <td><%# Eval("AmountRequired") %></td>
                             <td><%# Eval("ExpectedDays") %></td>
                             <td><%# Eval("CategoryID") %></td>
                             <td><%# Eval("Province") %></td>
                             <td><%# Eval("City") %></td>
                             <td><%# Eval("ProjectBrief") %></td>
                             <td><%# Eval("ProjectDetails") %></td>
                             <%--通过状态--%>
                             <td>
                                 <asp:Label ID="Label1" runat="server" ForeColor ='<%# int.Parse(Eval("PassState").ToString())==0?System.Drawing.Color.Red:System.Drawing.Color.Green %>'>
                                 <%# int.Parse(Eval("PassState").ToString())==0?"未通过":"通过" %>
                                     </asp:Label>
                             </td>
                             <td>
                                 <a href ="UpdatePassState.aspx?ProId=<%# Eval("ProId") %>&PassState=<%# Eval("PassState") %>"
                                  style='color:<%# int.Parse(Eval("PassState").ToString())==0?"green":"red" %>'>
                                     <%# int.Parse(Eval("PassState").ToString())==0?"通过":"不予通过" %>
                                 </a>
                             </td>
                         </tr>
                     </ItemTemplate>
                 </asp:Repeater>
             </table>
               <table style="width: 520px; height: 32px">
            <tr>
                <td colspan="2" style="width: 436px; height: 39px">
        当前第<asp:Label ID="lblCurrentPage" runat="server" Width="48px"></asp:Label>页 共<asp:Label
                        ID="lblCountPage" runat="server"></asp:Label>页</td>
                <td colspan="2" style="width: 34px; height: 39px">
                    <asp:Button ID="btnFirstPage" runat="server" Text="首页"  OnClick="btnFirstPage_Click"/></td>
                <td style="width: 18px; height: 39px">
                    <asp:Button ID="btnUpPage" runat="server" Text="上一页"  OnClick="btnUpPage_Click"/></td>
                <td style="width: 27px; height: 39px">
                    <asp:Button ID="btnNextPage" runat="server" Text="下一页" Onclick="btnNextPage_Click"/></td>
                <td style="width: 17px; height: 39px">
                    <asp:Button ID="btnLastPage" runat="server" EnableTheming="True" Text="尾页"  Onclick="btnLastPage_Click"/></td>
            </tr>
        </table>
         </div>
    </form>
</body>
</html>

后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;

namespace LoveFundWeb.后台
{
    public partial class ProjectList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lblCurrentPage.Text = "1";
                BindPoruduct();
                
            }
        }

        private void BindPoruduct()
        {
            int curPage = Convert.ToInt32(this.lblCurrentPage.Text);//当前第一页
            //创建了PagedDataSource对象
            PagedDataSource pds = new PagedDataSource();
            //获取数据源
             pds.DataSource = ProjectManage.GetAllProjects();
            pds.AllowPaging = true;         //允许分页功能
            pds.PageSize = 2;               //每页显示的行数
            pds.CurrentPageIndex = curPage - 1;               //当前的页数
            this.lblCountPage.Text = Convert.ToString(pds.PageCount);       //总页数
            this.btnFirstPage.Enabled = true;
            this.btnUpPage.Enabled = true;
            this.btnNextPage.Enabled = true;
            this.btnLastPage.Enabled = true;
            if (curPage == 1)
            {
                this.btnFirstPage.Enabled = false;
                this.btnUpPage.Enabled = false;
            }
            if (pds.PageCount == curPage)
            {
                this.btnFirstPage.Enabled = true;
                this.btnUpPage.Enabled = true;
                this.btnLastPage.Enabled = false;
                this.btnNextPage.Enabled = false;
            }
            //绑定数据源
            rpProjectList.DataSource = pds;
            rpProjectList.DataBind();
        }

        protected void btnFirstPage_Click(object sender, EventArgs e)
        {
            this.lblCurrentPage.Text = "1";
            this.BindPoruduct();
        }

        protected void btnUpPage_Click(object sender, EventArgs e)
        {
            this.lblCurrentPage.Text = Convert.ToString(Convert.ToInt32(this.lblCurrentPage.Text) - 1);
            this.BindPoruduct();
        }

        protected void btnNextPage_Click(object sender, EventArgs e)
        {
            this.lblCurrentPage.Text = Convert.ToString(Convert.ToInt32(this.lblCurrentPage.Text) + 1);
            this.BindPoruduct();
        }

        protected void btnLastPage_Click(object sender, EventArgs e)
        {
            this.lblCurrentPage.Text = Convert.ToString(this.lblCountPage.Text);
            this.BindPoruduct();
        }
    }
}

  • 9
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
ASP.NET实现分页功能可以通过以下步骤实现: 1. 在后端代码中获取数据总数和每页显示的记录数。 2. 根据获取到的数据总数和每页显示的记录数计算出总页数。 3. 根据当前页码和每页显示的记录数获取要显示的数据。 4. 将获取到的数据绑定到前端控件上进行显示。 5. 在前端页面中添加分页控件,并设置当前页、总页数、每页显示的记录数等属性。 6. 添加事件处理程序,根据用户的操作重新获取数据并进行显示。 以下是一个使用ASP.NET Web Forms实现分页功能的示例代码: 后端代码: ```c# protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindData(); } } private void BindData() { int currentPage = 1;//当前页码 int pageSize = 10;//每页显示的记录数 string sql = "select count(*) from Products";//获取数据总数的SQL语句 int totalCount = Convert.ToInt32(DbHelperSQL.GetSingle(sql));//获取数据总数 int totalPages = (int)Math.Ceiling((double)totalCount / pageSize);//计算总页数 if (!string.IsNullOrEmpty(Request.QueryString["page"])) { currentPage = Convert.ToInt32(Request.QueryString["page"]);//获取当前页码 } int startIndex = (currentPage - 1) * pageSize + 1;//计算要显示的数据的起始位置 int endIndex = currentPage * pageSize;//计算要显示的数据的结束位置 sql = "select * from (select row_number() over (order by ProductID) as RowNumber, * from Products) as Temp where Temp.RowNumber between " + startIndex + " and " + endIndex;//获取要显示的数据的SQL语句 DataTable dt = DbHelperSQL.Query(sql).Tables[0];//获取要显示的数据 rptProducts.DataSource = dt;//将获取到的数据绑定到前端控件上进行显示 rptProducts.DataBind(); //设置分页控件的属性 pager.CurrentPageIndex = currentPage; pager.PageSize = pageSize; pager.RecordCount = totalCount; pager.Visible = true; } protected void pager_PageChanged(object sender, EventArgs e) { BindData();//重新获取数据并进行显示 } ``` 前端代码: ```html <asp:Repeater ID="rptProducts" runat="server"> <ItemTemplate> <div><%# Eval("ProductName") %></div> </ItemTemplate> </asp:Repeater> <asp:DataPager ID="pager" runat="server" OnPreRender="pager_PreRender" OnPagerCommand="pager_PageChanged"> <Fields> <asp:NumericPagerField ButtonCount="10" /> </Fields> </asp:DataPager> ``` 注意事项: 1. 在获取数据总数时,可以使用SQL语句或者ORM框架等方式。 2. 在获取要显示的数据时,需要使用类似于ROW_NUMBER() OVER (ORDER BY xxx)的方式来进行分页。 3. 在分页控件的事件处理程序中,需要重新获取数据并进行显示。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值