首页 技术 正文
技术 2022年11月14日
0 收藏 437 点赞 4,439 浏览 8373 个字

Asp.Net WebForm 分页

一、 前言

Asp.Net WebForm 内置的DataPager让人十分蛋疼

本文使用的分页控件是第三方分页控件 AspNetPager,下载地址:

链接: http://pan.baidu.com/s/1eQJ2HR8 密码: aost

相关属性及其效果图:

【Asp.Net WebFrom】分页

二、使用第三方分页控件 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;
}
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,581
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918