zoukankan      html  css  js  c++  java
  • .Net工具 使用动软代码生成器快速生成可分页的GridView

    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="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也可以用这种方法绑定,用熟了就会很方便
  • 相关阅读:
    手机端和电脑端左右分屏录制视频解决方法
    收藏 网站部署配置文章
    廖雪峰网站:学习python函数—递归函数(四)
    廖雪峰网站:学习python函数—函数参数(三)
    廖雪峰网站:学习python函数—定义函数(二)
    廖雪峰网站:学习python函数—调用函数(一)
    廖雪峰网站:学习python基础知识—循环(四)
    廖雪峰网站:学习python基础知识—判断(三)
    Java提高十七:TreeSet 深入分析
    Java提高十六:TreeMap深入分析
  • 原文地址:https://www.cnblogs.com/lzhdim/p/1364572.html
Copyright © 2011-2022 走看看