解析asp.net的分页控件

所属分类: 网络编程 / ASP.NET 阅读数: 1565
收藏 0 赞 0 分享

一、说明

  AspNetPager.dll这个分页控件主要用于asp.net webform网站,现将整理代码如下

二、代码

1、首先在测试页面Default.aspx页面添加引用

<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>

2、写一个Repeater列表控件用于显示数据

<asp:Repeater ID="rptNews" runat="server">
  <ItemTemplate>
    <li>
      <span><%# Eval("time") %></span>
      <a href="NewsShow.aspx?id=<%# Eval("id") %>"><%# Access.GetStringNum( Eval("name").ToString(),15) %></a>
    </li>
  </ItemTemplate>
</asp:Repeater>

3、添加

<webdiyer:AspNetPager ID="AspNetPager1" runat="server" AlwaysShow="True" CustomInfoStyle="FONT-SIZE: 12px"
    FirstPageText="首页" HorizontalAlign="Center" inputboxstyle="width:19px" LastPageText="尾页"
    meta:resourcekey="AspNetPager1" NextPageText="下一页" PageSize="10" PrevPageText="上一页"
    Style="font-size: 14px" Width="95%" CssClass="anpager" CurrentPageButtonClass="cpb"
    OnPageChanging="AspNetPager1_PageChanging" ShowBoxThreshold="10">
</webdiyer:AspNetPager>

PageSize属性是用于设置每页显示的数量

4、后台代码绑定

//测试数据源<br>private void ShowNews()
{
  String strSql = String.Format("select * from News order by time asc");
  DataTable dtbl = Access.ExecuteDataTable(strSql, null);
  this.rptNews.DataSource = Access.GetPageDataSource(AspNetPager1, AspNetPager1.CurrentPageIndex - 1, dtbl);
  this.rptNews.DataBind();
}

Access是测试数据库访问类,在最后的Demo中提供给大家

5、分页控件点击页码事件

//分页
protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
{
  this.AspNetPager1.CurrentPageIndex = e.NewPageIndex;
  ShowNews();
} 
 

最后奉上整页代码:

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %>
<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <title>asp.net分页控件</title>
 <link href="css/css.css" rel="stylesheet" type="text/css" />
 <style type="text/css">
 .aboutcontentnr{width:100%; height:auto; }
 </style>
</head>
<body>
 <form id="form1" runat="server">
 <div>
  <ul class="news">
   <asp:Repeater ID="rptNews" runat="server">
    <ItemTemplate>
     <li><span>
      <%# Eval("time") %></span><a href="NewsShow.aspx?id=<%# Eval("id") %>"><%# Access.GetStringNum( Eval("name").ToString(),15) %></a></li>
    </ItemTemplate>
   </asp:Repeater>
  </ul>
  <div class="paginator">
   <webdiyer:AspNetPager ID="AspNetPager1" runat="server" AlwaysShow="True" CustomInfoStyle="FONT-SIZE: 12px"
    FirstPageText="首页" HorizontalAlign="Center" inputboxstyle="width:19px" LastPageText="尾页"
    meta:resourcekey="AspNetPager1" NextPageText="下一页" PageSize="10" PrevPageText="上一页"
    Style="font-size: 14px" Width="95%" CssClass="anpager" CurrentPageButtonClass="cpb"
    OnPageChanging="AspNetPager1_PageChanging" ShowBoxThreshold="10">
   </webdiyer:AspNetPager>
  </div>
 </div>
 </form>
</body>
</html>

Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class Default : System.Web.UI.Page
{
 protected void Page_Load(object sender, EventArgs e)
 {
  if (!IsPostBack)
  {
   ShowNews();
  }
 }
 //测试数据源
 private void ShowNews()
 {
  String strSql = String.Format("select * from News order by time asc");
  DataTable dtbl = Access.ExecuteDataTable(strSql, null);
  this.rptNews.DataSource = Access.GetPageDataSource(AspNetPager1, AspNetPager1.CurrentPageIndex - 1, dtbl);
  this.rptNews.DataBind();
 }
 //分页点击页码事件
 protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
 {
  this.AspNetPager1.CurrentPageIndex = e.NewPageIndex;
  ShowNews();
 }
}

三、Demo

  AspNetPager(dll)

  AspNetPage(Demo)

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,同时也希望多多支持脚本之家!

更多精彩内容其他人还在看

.NET Core源码解析配置文件及依赖注入

这篇文章我们设计了一些复杂的概念,因为要对ASP.NET Core的启动及运行原理、配置文件的加载过程进行分析,依赖注入,控制反转等概念的讲解等
收藏 0 赞 0 分享

.NET Corek中Git的常用命令及实战演练

这篇文章将通过故事的形式从Git的历史谈起,并讲述Git的强大之处。然后通过实战演练教你如何在Github以及码云上托管我们的代码并进行代码的版本控制
收藏 0 赞 0 分享

Asp.Net Core WebAPI使用Swagger时API隐藏和分组详解

这篇文章主要给大家介绍了关于Asp.Net Core WebAPI使用Swagger时API隐藏和分组的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Asp.Net Core具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

如何利用FluentMigrator实现数据库迁移

这篇文章主要给大家介绍了关于如何利用FluentMigrator实现数据库迁移的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

ASP.NET Core利用Jaeger实现分布式追踪详解

这篇文章主要给大家介绍了关于ASP.NET Core利用Jaeger实现分布式追踪的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用ASP.NET Core具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

浅谈从ASP.NET Core2.2到3.0你可能会遇到这些问题

这篇文章主要介绍了ASP.NET Core2.2到3.0可能会遇到的问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解.net core webapi 前后端开发分离后的配置和部署

这篇文章主要介绍了.net core webapi 前后端开发分离后的配置和部署,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解ASP.Net Core 中如何借助CSRedis实现一个安全高效的分布式锁

这篇文章主要介绍了ASP.Net Core 中如何借助CSRedis实现一个安全高效的分布式锁,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

.net 4.5部署到docker容器的完整步骤

这篇文章主要给大家介绍了关于.net 4.5部署到docker容器的完整步骤,文中通过示例代码介绍的非常详细,对大家学习或者使用.net4.5具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

.net core并发下线程安全问题详解

这篇文章主要给大家介绍了关于.net core并发下线程安全问题的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用.net core具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享
查看更多