.Net工具 - 使用动软代码生成器快速生成可分页的GridView

简介: GridView虽然自带了分页功能,但我们还是习惯自己来取需要的数据,在此使用了AspNetPager控件,也是免费的,当然不是广告了,也是实验了不少控件后选出来比较适合自己用的,结合代码生成器可快速生成可分页的GridView AspNetPager官方站点:可下载到dll及源代码http://www.
GridView虽然自带了分页功能,但我们还是习惯自己来取需要的数据,在此使用了AspNetPager控件,也是免费的,当然不是广告了,也是实验了不少控件后选出来比较适合自己用的,结合代码生成器可快速生成可分页的GridView

AspNetPager官方站点:可下载到dll及源代码
http://www.webdiyer.com/AspNetPager/default.aspx

分页存储过程如下:根据铁拳的代码稍微修改了一下

USE [××数据库名]
GO
/****** 对象: StoredProcedure [dbo].[GetRecordFromPage] 脚本日期: 03/18/2008 13:35:43 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
/*
函数名称: GetRecordFromPage
函数功能: 获取指定页的数据
参数说明: @tblName 包含数据的表名
@fldName 关键字段名
@PageSize 每页记录数
@PageIndex 要获取的页码
@OrderType 排序类型, 0 - 升序, 1 - 降序
@strWhere 查询条件 (注意: 不要加 where)
作  者: 铁拳
创建时间: 2004-07-04
修改时间: 2007-07-04
*/
-- =============================================
CREATE PROCEDURE [dbo].[GetRecordFromPage]
@tblName varchar(255), -- 表名
@fldName varchar(255), -- 字段名
@PageSize int = 10, -- 页尺寸
@PageIndex int = 1, -- 页码
@IsReCount bit = 0, -- 返回记录总数, 非 0 值则返回
@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序
@strWhere varchar(2000) = '' -- 查询条件 (注意: 不要加 where)
AS

declare @strSQL varchar(6000) -- 主语句
declare @strTmp varchar(1000) -- 临时变量
declare @strOrder varchar(500) -- 排序类型

if @OrderType != 0
begin
set @strTmp = '<(select min'
set @strOrder = ' order by [' + @fldName + '] desc'
end
else
begin
set @strTmp = '>(select max'
set @strOrder = ' order by [' + @fldName +'] asc'
end

set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @fldName + '] from [' + @tblName + ']' + @strOrder + ') as tblTmp)'
+ @strOrder

if @strWhere != ''
set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @fldName + '] from [' + @tblName + '] where (' + @strWhere + ') '
+ @strOrder + ') as tblTmp) and (' + @strWhere + ') ' + @strOrder

if @PageIndex = 1
begin
set @strTmp = ''
if @strWhere != ''
set @strTmp = ' where (' + @strWhere + ')'

set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
+ @tblName + ']' + @strTmp + ' ' + @strOrder
end

if @IsReCount != 0
set @strSQL = 'select count(*) as Total from [' + @tblName + ']'+' where ' + @strWhere

exec (@strSQL)


步骤:
1、DAL层中的分页代码如下:

///
/// 分页获取数据列表
///
/// 每页数量
/// 当前页索引
/// 查询字符串
/// 设置排序类型, 非 0 值则降序
///
public DataSet GetList(int PageSize, int PageIndex, string strWhere, string OrderType)
{
SqlParameter[] parameters = {
new SqlParameter("@tblName", SqlDbType.VarChar, 255),
new SqlParameter("@fldName", SqlDbType.VarChar, 255),
new SqlParameter("@PageSize", SqlDbType.Int),
new SqlParameter("@PageIndex", SqlDbType.Int),
new SqlParameter("@IsReCount", SqlDbType.Bit),
new SqlParameter("@OrderType", SqlDbType.Bit),
new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
};
parameters[0].Value = "Accounts_Users";
//2007.11.22 这里修改为按UserName排序
parameters[1].Value = "UserName";
parameters[2].Value = PageSize;
parameters[3].Value = PageIndex;
parameters[4].Value = 0;
parameters[5].Value = int.Parse(OrderType);
parameters[6].Value = strWhere;
return DbHelperSQL.RunProcedure("GetRecordFromPage", parameters, "ds");
}


BLL层中:

///
/// 分页获得数据列表
///
/// 每页数量
/// 当前页索引
/// 查询字符串
/// 设置排序类型, 非 0 值则降序
///
public DataSet GetList(int PageSize, int PageIndex, string strWhere, string OrderType)
{
return dal.GetList(PageSize, PageIndex, strWhere, OrderType);
}


2、在页面上拖一个GridView控件,拖一个AspNetPager控件,
如下:

<webdiyer:AspNetPager id=AspNetPager
CustomInfoHTML="共%RecordCount%条记录 Page %CurrentPageIndex% of %PageCount%  Order %StartRecordIndex%-%EndRecordIndex%"
FirstPageText="首页" HorizontalAlign="Center" InputBoxStyle="width:19px" LastPageText="尾页"
meta:resourcekey="AspNetPager" NextPageText="后页" OnPageChanged="AspNetPager_PageChanged"
PageSize="10" PrevPageText="前页" ShowCustomInfoSection="Left" ShowInputBox="Always"
Style="font-size: 12px" Width="90%">


3、在cs文件中增加以下代码:
PageLoad事件中:

if (!this.IsPostBack)
{
//确定数据数量
HDHG.BLL.Accounts.Accounts_Users blluser = new HDHG.BLL.Accounts.Accounts_Users();
this.AspNetPager.RecordCount = blluser.GetCount(strWhere);
BindGridView();
}


绑定数据函数

///
/// 绑定用户列表
///
private void BindGridView()
{
strWhere = this.lblStrWhere.Text;
HDHG.BLL.Accounts.Accounts_Users blluser = new HDHG.BLL.Accounts.Accounts_Users();
DataSet ds = blluser.GetList(this.AspNetPager.PageSize, this.AspNetPager.CurrentPageIndex, strWhere, "0");
this.myGridView.DataSource = ds;
this.myGridView.DataBind();
}


分页绑定数据:

///
/// 分页绑定数据
///
///
///
protected void AspNetPager_PageChanged(object sender, EventArgs e)
{
this.BindGridView();
}


这样就可以分页了

注:blluser.GetCount()是获取数据数量的方法
lblStrWhere是一个隐藏Label,保存查询条件。也可以不设置这个控件
DataList也可以用这种方法绑定,用熟了就会很方便
目录
相关文章
|
7月前
|
Shell 网络安全 C#
一款 .NET 开源、功能强大的远程连接管理工具,支持 RDP、VNC、SSH 等多种主流协议!
一款 .NET 开源、功能强大的远程连接管理工具,支持 RDP、VNC、SSH 等多种主流协议!
201 4
|
5月前
|
缓存 开发框架 .NET
一个功能丰富的 .NET 工具库 XiHan.Framework.Utils
XiHan.Framework.Utils 是一个功能全面的 .NET 工具库,包含字符串处理、集合扩展、加密解密、分布式 ID、文件操作、缓存、线程、国际化等模块。设计上注重高内聚、低耦合,适用于各类 .NET 应用开发。支持 AES 加密、树形结构转换、分页过滤、日志输出等功能,提供简单易用的 API。可通过 NuGet 快速安装,源码开放,采用 MIT 协议。
194 56
|
10月前
|
XML C# 数据格式
一个.NET开源、免费、功能强大的 PDF 处理工具
一个.NET开源、免费、功能强大的 PDF 处理工具
257 8
|
10月前
|
网络协议 C#
基于.NET WinForm开发的一款硬件及协议通讯工具
基于.NET WinForm开发的一款硬件及协议通讯工具
105 7
|
10月前
|
C# UED
一个.NET开源、易于使用的屏幕录制工具
一个.NET开源、易于使用的屏幕录制工具
108 6
|
10月前
|
Web App开发 C# Windows
一款.NET开源的Windows资源管理器标签页工具
一款.NET开源的Windows资源管理器标签页工具
128 5
|
11月前
|
机器学习/深度学习 文字识别 并行计算
一款.NET开源的屏幕实时翻译工具
一款.NET开源的屏幕实时翻译工具
215 0
|
10月前
|
监控 前端开发 API
一款基于 .NET MVC 框架开发、功能全面的MES系统
一款基于 .NET MVC 框架开发、功能全面的MES系统
237 5
|
开发框架 前端开发 .NET
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
385 0
|
开发框架 前端开发 JavaScript
ASP.NET MVC 教程
ASP.NET 是一个使用 HTML、CSS、JavaScript 和服务器脚本创建网页和网站的开发框架。
199 7

热门文章

最新文章