• ASP.NET真假分页—真分页


    当数据量过大,有几万甚至十几万条数据时,每次都从数据库中取出所有数据就会降低查询效率,系统运行慢,还有可能卡死,这时假分页就会显得很不人性化,因此有了真分页的必要性。 

    正如上篇博文总结归纳,“真”相对于“假”存在,即不藕断丝连,从根部彻底断开,在此处表现为根据查询条件,只从数据库中提取出需要的部分,适合于大数据。而真分页的实现要借助于第三方控件AspNetPager。 

    AspNetPager控件是一个基于.net的第三方免费开源控件,具有开发高效、使用方便、功能完整等优点。它弥补了GridView内置分页以及PageDatasource类辅助分页的不足,将分页数据逻辑和页面UI分离开来,非常有利于SQL分页的实现。 

    首先需要下载AspNetPager控件:http://www.webdiyer.com/downloads/

    data-cke-saved-src=http://www.2cto.com/uploadfile/Collfiles/20140827/20140827084908143.png

    在VS中引用AspNetPager控件,欢迎参考博文《VS添加Ajax》中添加选择项部分(有图有真相),此处不再赘述:http://blog.csdn.net/u010773667/article/details/38518461

    首先在web窗体中拖放一个gridview控件用来显示数据,选中AspNetPager控件拖拽到web窗体相应位置用来进行分页设置。显示如下:

    data-cke-saved-src=http://www.2cto.com/uploadfile/Collfiles/20140827/20140827084908147.png

    对分页提示信息进一步改进。选中spNetPager控件,在右下角将会显现一个小按钮,单击打开,对导航按钮显示文本进行设置

    data-cke-saved-src=http://www.2cto.com/uploadfile/Collfiles/20140827/20140827084908148.png

    data-cke-saved-src=http://www.2cto.com/uploadfile/Collfiles/20140827/20140827084908149.png

    改进效果:

    data-cke-saved-src=http://www.2cto.com/uploadfile/Collfiles/20140827/20140827084909150.png

    对页索引文本或下拉框进行设置

    data-cke-saved-src=http://www.2cto.com/uploadfile/Collfiles/20140827/20140827084909151.png

    改进效果见下图:

    data-cke-saved-src=http://www.2cto.com/uploadfile/Collfiles/20140827/20140827084909152.png

    如果还想显示的更加具体,可进行自定义信息区显示方式及内容设置

    data-cke-saved-src=http://www.2cto.com/uploadfile/Collfiles/20140827/20140827084909153.png

      上述对控件进行的所有设置将在VS中自动生成相应代码,so我们也可以通过手动输入代码进行设置。

    data-cke-saved-src=http://www.2cto.com/uploadfile/Collfiles/20140827/20140827084909154.png

    好了,设置好了前台,接下来就要进行数据绑定了

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    protected void Page_Load(object sender, EventArgs e)
            {         
                if (!Page .IsPostBack )
                {
                    string caid = Request.QueryString[caid];
                    DataTable dt = new NewsManager().SelectAllNewsByCaId(caid);            
                    anpCa.AlwaysShow = true;
                    anpCa.PageSize = 5;
                    anpCa.RecordCount = dt.Rows.Count;
                    int startIndex = anpCa.PageSize * 0;
                    int endIndex = anpCa.PageSize * 1;              
                    gvDataBind(caid, startIndex, endIndex);              
                }
            }
            private void gvDataBind(string caid,int startIndex,int endIndex)
            {          
                DataTable dt = new NewsManager().SelectPartNewsByCaId(caid, startIndex, endIndex);
                if (dt.Rows.Count != 0)
                {
                    lblCategory.Text = dt.Rows[0][name].ToString();  //使类别标题显示相应的类别名称
                }
                gvNew.DataSource = dt;
                gvNew.DataBind();
            }
          protected void anpCa_PageChanged(object sender, EventArgs e)
            {
                string caid = 6;
                int startIndex = anpCa.PageSize * (anpCa.CurrentPageIndex - 1)+1;
                int endIndex = anpCa.PageSize * (anpCa.CurrentPageIndex);
                gvDataBind(caid, startIndex, endIndex);
            }
    }

     在D层数据查询的部分代码展示

     
    #region 根据类别ID取出该类别下的所有新闻的分页显示
            /// 
            /// 根据类别ID取出该类别下的所有新闻
            /// 
            ///类别ID
            /// 
            public DataTable SelectPartNewsByCaId(string caId,int startIndex, int endIndex)
            {
    
                DataTable dt = new DataTable();
                SqlParameter[] paras = new SqlParameter[]
    {
               new SqlParameter (@caId,caId ),
               new SqlParameter (@startIndex,startIndex ),
               new SqlParameter (@endIndex,endIndex )
        };
                dt = sqlhelper.ExecuteQuery(dbo.category_showpage, paras, CommandType.StoredProcedure);
                return dt;
            }
            #endregion存储过程(很重要)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    -- =============================================
    -- Author:      王英群
    -- Create date: 2014-8-10
    -- Description: 跟据类别ID取出该类别下的所有新闻的分页显示
    -- =============================================
    ALTER PROCEDURE [dbo].[category_showpage]
        @caid int,
        @startIndex int,
        @endIndex int
    AS
    BEGIN
        with temptable as (
            select ROW_NUMBER() over (order by id desc) as 行号, * from
                (
                    select n.id,n.titile,n.createTime,c.[name],n.caId from news n
                     inner join category c on n.caId =c.id and n.caId =@caid
                ) as aa
        )
     
         
        select * from temptable  where 行号 between @startIndex and @endIndex
         
    END

    运行后效果见下图:

    data-cke-saved-src=http://www.2cto.com/uploadfile/Collfiles/20140827/20140827084909155.png

    注意:我的程序中多了一个参数(类别ID),在页索引动态变化的过程中需要一直重新获得,这一点我没有实现,希望小伙伴们可以帮助我,谢谢!

    结合上篇博文,假分页适合于数据量相对较小的情况下,而真分页适合于数据量大的情况下。真假分页的使用,为我们的阅读减负。

  • 相关阅读:
    NullPointerException
    面试oracle 经常问的一个问题- 事务
    python 之 import、from、as 关键字的 白话 解释与例子
    python 学习 之 第二章(条件、循环和其他语句)
    python学习 之 第一章 (简单例子与常用数据类型)
    python中常用函数含义记录
    python 2 版本中的input() 和 raw_input() 函数的比较
    字符串处理关键字str 和 repr
    文件操作-一个可以直接复制文件数据的小程序
    C语言 32个关键字
  • 原文地址:https://www.cnblogs.com/ranran/p/4105483.html
Copyright © 2020-2023  润新知