【Asp.Net WebFrom】分页
Asp.Net WebForm 分页
一、 前言
Asp.Net WebForm 内置的DataPager让人十分蛋疼
本文使用的分页控件是第三方分页控件 AspNetPager,下载地址:
链接: http://pan.baidu.com/s/1eQJ2HR8 密码: aost
相关属性及其效果图:

二、使用第三方分页控件 AspNetPager
第一步:显示未分页时的数据
前端代码:
注意:将ListView控件的EnableViewState="False"。说明即使是EnableViewState="False",不影响分页的实现。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ListView分页AspNetPager第三方控件.aspx.cs" Inherits="Focus.Asp.WebForm.Czbk.Focus.ListView.ListView分页AspNetPager第三方控件" %> <!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ListView ID="ListView1" runat="server" EnableViewState="False">
<%--asp.net 3.5的LayoutTemplate是必须的,asp.net 4.0的LayoutTemplate不是必须的--%>
<LayoutTemplate>
<div style="border:solid seagreen;">
<table>
<thead>
<tr>
<th>Id</th>
<th>种类</th>
</tr>
</thead>
<tbody>
<%--显示写LayoutTemplate时必须有一个占位符(可以是任意类型),
但是id必须是itemPlaceholder,项占位符控件还必须指定 runat="server"。--%>
<asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
</tbody>
</table>
</div>
</LayoutTemplate>
<ItemTemplate>
<div>
<tr>
<td><asp:Label runat="server"><%#Eval("Id") %></asp:Label></td>
<td><asp:Label runat="server"><%#Eval("Kind") %></asp:Label></td>
</tr>
</div>
</ItemTemplate>
<AlternatingItemTemplate>
<div >
<tr>
<td><asp:Label runat="server"><%#Eval("Id") %></asp:Label></td>
<td><asp:Label runat="server"><%#Eval("Kind") %></asp:Label></td>
</tr>
</div>
</AlternatingItemTemplate>
<EmptyDataTemplate>
抱歉,没有数据
</EmptyDataTemplate>
</asp:ListView> </div>
</form>
</body>
</html>
后台代码绑定数据:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
} private void BindData()
{
List<Electronics> dataSource = DataSourceTemplete.GetElectronics(); this.ListView1.DataSource = dataSource;
this.ListView1.DataBind(); }
其中 用静态类DataSourceTemplete来模拟数据源,数据源的结构类是Electronics,它们的代码如下:
DataSourceTemplete.cs:
using System;
using System.Collections.Generic;
using System.Data.Entity.Migrations.Model;
using System.Linq;
using System.Threading;
using System.Web;
using Focus.Asp.WebForm.Czbk.Focus.ValidateControl; namespace Focus.Asp.WebForm.Czbk.Focus.Helper
{
public static class DataSourceTemplete
{
private static List<Electronics> electronics = new List<Electronics>()
{
//new Electronics{Id = -1, Kind="--请选择--"},
new Electronics{Id = , Kind="电脑"},
new Electronics{Id = , Kind="手机"},
new Electronics{Id = , Kind="平板电脑"},
new Electronics{Id = , Kind="一体机"},
new Electronics{Id = , Kind="路由器"},
new Electronics{Id = , Kind="吹风机"},
new Electronics{Id = , Kind="CPU"},
new Electronics{Id = , Kind="散热器"},
new Electronics{Id = , Kind="主板"},
new Electronics{Id = , Kind="内存条"}, }; public static List<Electronics> GetElectronics()
{
return electronics;
} public static void Add(Electronics electronic)
{
electronics.Add(electronic);
} public static void Delete(int id)
{
var toDel = electronics.First(i => i.Id == id);
electronics.Remove(toDel);
} public static void Edit(Electronics electronic)
{
var toEdit = electronics.First(i => i.Id == electronic.Id);
toEdit.Kind = electronic.Kind;
} public static Electronics Get(int id)
{
return electronics.First(i => i.Id == id);
}
}
}
Electronics.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Focus.Asp.WebForm.Czbk.Focus.ValidateControl
{
public class Electronics
{
public int Id { get; set; }
public string Kind { get; set; }
}
}
第二步:添加分页控件
先在项目中引入AspNetPager.dll;
前端代码中添加添加分页控件,
<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="aspNetPager" %> ....
<form id="form1" runat="server">
<div>
<asp:ListView ID="ListView1" runat="server" EnableViewState="False">
.....
</asp:ListView>
<div>
<aspNetPager:AspNetPager runat="server" ID="AspNetPager1"
AlwaysShow="True"
OnPageChanged="AspNetPager1_OnPageChanged"
UrlPaging="True"
NumbericButtonCount ="5"
NumericButtonTextFormatString="[{0}]"
ShowCustomInfoSection="Right"
ShowPrevNext="True"
ShowPageIndex="True"
ShowFirstLast="True"
FirstPageText="首页"
LastPageText="末页"
PrevPageText="上一页"
NextPageText="下一页"
ShowNavigationToolTip="True"
TextBeforeInputBox="第"
ShowInputBox='Always'
TextAfterInputBox="页"
SubmitButtonText="确认"> </aspNetPager:AspNetPager>
</div>
</div>
</form>
第三步:为分页控件添加OnPageChanged事件处理:
其中,第一步中BindData();也相应的添加相关代码,以实现分页相关逻辑。
后台代码:
private int pageIndex;
private int pageSize = Convert.ToInt32(ConfigurationManager.AppSettings["pageSize"]);
private int totalCount; ........ protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
{
BindData(); /*设置分页控件属性*/
this.AspNetPager1.CustomInfoHTML = "记录总数:<b>" + AspNetPager1.RecordCount.ToString() + "</b>";
this.AspNetPager1.CustomInfoHTML += " 总页数:<b>" + AspNetPager1.PageCount.ToString() + "</b>";
this.AspNetPager1.CustomInfoHTML += " 当前页:<font color=\"red\"><b>" + AspNetPager1.CurrentPageIndex.ToString() + "</b></font>";
} private void BindData()
{
List<Electronics> dataSource = DataSourceTemplete.GetElectronics(); pageIndex = this.AspNetPager1.CurrentPageIndex;
totalCount = DataSourceTemplete.GetElectronics().Count; this.ListView1.DataSource = dataSource.Skip((pageIndex-1) * pageSize)
.Take(pageSize)
.ToList();
this.ListView1.DataBind(); /*设置分页控件属性*/
this.AspNetPager1.RecordCount = totalCount;//所有数据的总数,必须设置该值。
this.AspNetPager1.PageSize = pageSize;
}
其中,在web.config文件中配置PageSize(每页显示多少项数据):
<configuration>
<appSettings>
<add key="pageSize" value=""/>
</appSettings>
并通过如下代码获取:
private int pageSize = Convert.ToInt32(ConfigurationManager.AppSettings["pageSize"]);
页面完整代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ListView分页AspNetPager第三方控件.aspx.cs" Inherits="Focus.Asp.WebForm.Czbk.Focus.ListView.ListView分页AspNetPager第三方控件" %>
<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="aspNetPager" %> <!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ListView ID="ListView1" runat="server" EnableViewState="False">
<%--asp.net 3.5的LayoutTemplate是必须的,asp.net 4.0的LayoutTemplate不是必须的--%>
<LayoutTemplate>
<div style="border:solid seagreen;">
<table>
<thead>
<tr>
<th>Id</th>
<th>种类</th>
</tr>
</thead>
<tbody>
<%--显示写LayoutTemplate时必须有一个占位符(可以是任意类型),
但是id必须是itemPlaceholder,项占位符控件还必须指定 runat="server"。--%>
<asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
</tbody>
</table>
</div>
</LayoutTemplate>
<ItemTemplate>
<div>
<tr>
<td><asp:Label runat="server"><%#Eval("Id") %></asp:Label></td>
<td><asp:Label runat="server"><%#Eval("Kind") %></asp:Label></td>
</tr>
</div>
</ItemTemplate>
<AlternatingItemTemplate>
<div >
<tr>
<td><asp:Label runat="server"><%#Eval("Id") %></asp:Label></td>
<td><asp:Label runat="server"><%#Eval("Kind") %></asp:Label></td>
</tr>
</div>
</AlternatingItemTemplate>
<EmptyDataTemplate>
抱歉,没有数据
</EmptyDataTemplate>
</asp:ListView>
<div>
<aspNetPager:AspNetPager runat="server" ID="AspNetPager1"
AlwaysShow="True"
OnPageChanged="AspNetPager1_OnPageChanged"
UrlPaging="True"
NumbericButtonCount ="3"
NumericButtonTextFormatString="[{0}]"
ShowCustomInfoSection="Right"
ShowPrevNext="True"
ShowPageIndex="True"
ShowFirstLast="True"
FirstPageText="首页"
LastPageText="末页"
PrevPageText="上一页"
NextPageText="下一页"
ShowNavigationToolTip="True"
TextBeforeInputBox="第"
ShowInputBox='Always'
TextAfterInputBox="页"
SubmitButtonText="确认"> </aspNetPager:AspNetPager>
</div>
</div>
</form>
</body>
</html>
后台完整代码:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Focus.Asp.WebForm.Czbk.Focus.Helper;
using Focus.Asp.WebForm.Czbk.Focus.ValidateControl; namespace Focus.Asp.WebForm.Czbk.Focus.ListView
{
public partial class ListView分页AspNetPager第三方控件 : System.Web.UI.Page
{
private int pageIndex;
private int pageSize = Convert.ToInt32(ConfigurationManager.AppSettings["pageSize"]);
private int totalCount; protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
} protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
{
BindData(); /*设置分页控件属性*/
this.AspNetPager1.CustomInfoHTML = "记录总数:<b>" + AspNetPager1.RecordCount.ToString() + "</b>";
this.AspNetPager1.CustomInfoHTML += " 总页数:<b>" + AspNetPager1.PageCount.ToString() + "</b>";
this.AspNetPager1.CustomInfoHTML += " 当前页:<font color=\"red\"><b>" + AspNetPager1.CurrentPageIndex.ToString() + "</b></font>";
} private void BindData()
{
List<Electronics> dataSource = DataSourceTemplete.GetElectronics(); pageIndex = this.AspNetPager1.CurrentPageIndex;
totalCount = DataSourceTemplete.GetElectronics().Count; this.ListView1.DataSource = dataSource.Skip(pageIndex- * pageSize)
.Take(pageSize)
.ToList();
;
this.ListView1.DataBind(); /*设置分页控件属性*/
this.AspNetPager1.RecordCount = totalCount;//所有数据的总数,必须设置该值。
this.AspNetPager1.PageSize = pageSize;
}
}
}
【Asp.Net WebFrom】分页的更多相关文章
- ASP.NET MVC分页组件MvcPager 2.0版发布暨网站全新改版
MvcPager分页控件是在ASP.NET MVC Web应用程序中实现分页功能的一系列扩展方法,该分页控件的最初的实现方法借鉴了网上流行的部分源代码, 尤其是ScottGu的PagedList< ...
- ASP.NET MVC分页实现之改进版-增加同一个视图可设置多个分页
我之前就已经实现了ASP.NET MVC分页(查看该博文),但它有局限性,必须确保在同一个视图中只能有一处分页,若需要在同一个视图中设置多个分页,却无能为力,为此,我重新对原先的代码进行了优化,增加了 ...
- 基于Bootstrap的Asp.net Mvc 分页
基于Bootstrap的Asp.net Mvc 分页的实现 最近写了一个mvc 的 分页,样式是基于 bootstrap 的 ,提供查询条件,不过可以自己写样式根据个人的喜好,以此分享一下.首先新建一 ...
- [Asp.net]AspNetPager分页组件
引言 在基于Asp.net的内网系统中,分页功能是最常用的,用的最多的组件就是AspNetPager. AspNetPager 官网:http://www.webdiyer.com/aspnetpag ...
- Asp.Net真分页技术
最近学校要做课题,闲来没事研究了下Asp.net的分页,我使用Repeater进行数据的绑定,每次从数据库读取到8条数据填充到Repeater中,这样搞可以降低数据库的压力,提高效率. 效果图如下: ...
- Asp.net+JS 分页
function pagestart() {//初始化页面,获取公司新闻 $("#pagediv").hide(); $("); var pagesize = $(&qu ...
- cPage分页,asp.net自定义分页,url传值分页,支持datalist、gridview、Repeater等
asp.net分页是最最常用的功能,实现方式也很多,使用不同的控件有不同的分页方式. 下面分享一个我们团队内部使用了多年的一个分页控件cPage,是自己设计编写,没有冗余,简单.快速. cPage,现 ...
- PagedDataSource、Repeater以及AspNetPager在ASP.NET上分页。
一.前台使用服务器标签 1.1使用Repeater控件 <asp:Repeater ID="Repeater1" runat="server"> & ...
- ASP.NET MVC分页实现
ASP.NET MVC中不能使用分页控件,所以我就自己写了一个分页局部视图,配合PageInfo类,即可实现在任何页面任意位置呈现分页,由于采用的是基于POST分页方式,所以唯一的限制就是必须放在FO ...
随机推荐
- QQ分组实现,可收缩---ExpandableListView
activity: package com.zzw.qqgroup; import java.util.ArrayList; import java.util.HashMap; import java ...
- [CentOS 7] 安装nginx第一步先搭建nginx服务器环境
简要地介绍一下,如何在CentOS 7中安装nginx服务器 方法/步骤 下载对应当前系统版本的nginx包(package) # wget http://nginx.org/packages/ ...
- 007-python基础-pyc是什么
3.1 解释型语言和编译型语言 计算机是不能够识别高级语言的,所以当我们运行一个高级语言程序的时候,就需要一个"翻译机"来从事把高级语言转变成计算机能读懂的机器语言的过程.这个过程 ...
- SQL Server基本操作积累
一.基本操作 1.将数据绑定到DataGridVirw控件上显示的数据列标题将会是数据库中的字段名称,可以在使用select语句时使用AS关键字将转化为列名的别名 select name AS 姓名 ...
- Oracle出现字符集问题处理方法
1. Cmd进去DOS 2. 再输入dbca(database create) 3. 弹出的界面,直接下一步,选择删除数据库 4. 成功删除后,回到一第一界面,选择创建数据库,下一步. 5. ...
- Android WIFI 启动流程(TIP^^)
前几天因为解决一堆Bug,没时间写.我不会每天都写,就是为了存档一些资料. 内容来源:工作中接触到的+高手博客+文档(Books)=自己理解 仅限参考^^ 此博客是上一个<<Android ...
- 关于table元素的认识
表格是网页上最常见的元素,但是,现在对很多刚入行的前端们那是谈table色变.那是为啥?这是表格的框架的简单.明了.在传统的网页中使用没有边框的表格来排版是非常流行.在web标准逐渐深入设计领域以后, ...
- php之常用函数库
1.时间和日期 如何获取时间戳 time()--从1970年开始计算的毫秒数 echo time(); 日期 echo date('Y-m-d H:i:s'); 获取默认是时区 echo date_d ...
- MVC4.0 解决Controllers与Areas中控制器不能同名问题
在使用MVC4.0的时候,难免会遇到在根目录下的Controllers中添加的控制器名称可能会跟在Areas中的某个区域下的控制器名称一样.这个时候访问Areas下面的Controller/Actio ...
- MongoDB学习笔记-数据格式及数据类型
JSON JSON是一种简单的数据表示方式,它易于理解.易于解析.易于记忆.但从另一方面来说,因为只有null.布尔.数字.字符串.数组和对象这几种数据类型,所以JSON有一定局限性.例如,JSON没 ...