Asp.Net DataTable 简单分页(新)

		/// <summary>

		/// DataTable分页方法,和其他的方法比较优势为不需要DataTable.Copy()操作,

		/// 节约了一定的资源。By Adchun

		/// </summary>

		/// <param name="dataTable"></param>

		/// <param name="curPageNo"></param>

		/// <param name="pageSize"></param>

		/// <param name="rowCount"></param>

		/// <param name="maxPageNo"></param>

		/// <param name="prePageNo"></param>

		/// <param name="nextPageNo"></param>

		public static void SetTablePage

			(

			ref DataTable dataTable,	//要分页的表

			ref int curPageNo,	//当前页码,从1开始

			int pageSize,	//每页记录数

			ref int rowCount,	//返回记录总数

			ref int maxPageNo,	//返回最大页码

			ref int prePageNo,	//返回当前页的上一页码

			ref int nextPageNo	//返回当前页的下一页码

			)

		{

			rowCount = dataTable.Rows.Count;

			maxPageNo = rowCount % pageSize == 0 ? rowCount / pageSize : rowCount / pageSize + 1;



			//以下这句为防止curPageNo出界,可有可无

			curPageNo = curPageNo < 1 ? curPageNo = 1 : curPageNo > maxPageNo ? curPageNo = maxPageNo : curPageNo;



			prePageNo = curPageNo - 1 == 0 ? curPageNo : curPageNo - 1;

			nextPageNo = curPageNo + 1 > maxPageNo ? maxPageNo : curPageNo + 1;



			//从上往下删除表中不需要的记录

			int cycTimes = (curPageNo - 1) * pageSize;

			for (int i = 0; (dataTable.Rows.Count > 0 && i < cycTimes); i++)

			{

				dataTable.Rows.RemoveAt(0);

			}



			//从下往上删除表中不需要的记录,i值减少的同时也意味着Rows.Count值的减少

			for (int i = dataTable.Rows.Count; i > pageSize; i--)

			{

				dataTable.Rows.RemoveAt(i - 1);

			}

		}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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. 在分页控件的事件处理程序中,需要重获取数据并进行显示。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值