您的位置:首页 > 编程语言

[转]使用代码生成器快速生成可分页的GridView

2008-04-03 23:21 295 查看
GridView虽然自带了分页功能,但我们还是习惯自己来取需要的数据,在此使用了AspNetPager控件,也是免费的,当然不是广告了,也是实验了不少控件后选出来比较适合自己用的,结合代码生成器可快速生成可分页的GridView

AspNetPager官方站点:可下载到dll及源代码
http://www.webdiyer.com/AspNetPager/default.aspx
分页存储过程如下:根据铁拳的代码稍微修改了一下

[复制到剪贴板]CODE:

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层中的分页代码如下:

[复制到剪贴板]CODE:

///

/// 分页获取数据列表

///

/// 每页数量

/// 当前页索引

/// 查询字符串

/// 设置排序类型, 非 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层中:

[复制到剪贴板]CODE:

///

/// 分页获得数据列表

///

/// 每页数量

/// 当前页索引

/// 查询字符串

/// 设置排序类型, 非 0 值则降序

///

public DataSet GetList(int PageSize, int PageIndex, string strWhere, string OrderType)

{

return dal.GetList(PageSize, PageIndex, strWhere, OrderType);

}

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

如下:

[复制到剪贴板]CODE:

<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事件中:

[复制到剪贴板]CODE:

if (!this.IsPostBack)

{

//确定数据数量

HDHG.BLL.Accounts.Accounts_Users blluser = new HDHG.BLL.Accounts.Accounts_Users();

this.AspNetPager.RecordCount = blluser.GetCount(strWhere);

BindGridView();

}

绑定数据函数

[复制到剪贴板]CODE:

///

/// 绑定用户列表

///

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();

}

分页绑定数据:

[复制到剪贴板]CODE:

///

/// 分页绑定数据

///

///

///

protected void AspNetPager_PageChanged(object sender, EventArgs e)

{

this.BindGridView();

}

这样就可以分页了

注:blluser.GetCount()是获取数据数量的方法

lblStrWhere是一个隐藏Label,保存查询条件。也可以不设置这个控件

DataList也可以用这种方法绑定,用熟了就会很方便
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: