ASP.NET真假分页—真分页

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


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



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

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


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


首先在web窗体中拖放一个gridview控件用来显示数据,选中AspNetPager控件拖拽到web窗体相应位置用来进行分页设置。选中spNetPager控件,在右下角将会显现一个小按钮,单击打开,对导航按钮显示文本进行设置。



运行后效果:


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


改进效果见下图:


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



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


好了,设置好了前台,接下来就要进行数据绑定了(注意:在方法anpCa_PageChanged()中绑定了caid=6,此处需要重新获得类别ID,我没有解决。。。希望会的朋友友情提示一下)


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

  1. </pre></p><p></p><pre class="html" name="code"> 
</pre></p><p></p><pre class="html" name="code">
  1. </pre><span style="font-family:华文楷体;font-size: 14pt;"></span><pre class="html" name="code">#region 根据类别ID取出该类别下的所有新闻的分页显示 
  2.         /// <summary> 
  3.         /// 根据类别ID取出该类别下的所有新闻 
  4.         /// </summary> 
  5.         /// <param name="caId">类别ID</param> 
  6.         /// <returns></returns> 
  7.         public DataTable SelectPartNewsByCaId(string caId,int startIndex, int endIndex) 
  8.         { 
  9.  
  10.             DataTable dt = new DataTable(); 
  11.             SqlParameter[] paras = new SqlParameter[] 
  12.            new SqlParameter ("@caId",caId ), 
  13.            new SqlParameter ("@startIndex",startIndex ), 
  14.            new SqlParameter ("@endIndex",endIndex ) 
  15.     }; 
  16.             dt = sqlhelper.ExecuteQuery("dbo.category_showpage", paras, CommandType.StoredProcedure); 
  17.             return dt; 
  18.         } 
  19.         #endregion 
</pre><span style="font-family:华文楷体;font-size: 14pt;"></span><pre class="html" name="code">#region 根据类别ID取出该类别下的所有新闻的分页显示
        /// <summary>
        /// 根据类别ID取出该类别下的所有新闻
        /// </summary>
        /// <param name="caId">类别ID</param>
        /// <returns></returns>
        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. -- Author:      王英群 
  3. -- Create date: 2014-8-10 
  4. -- Description: 跟据类别ID取出该类别下的所有新闻的分页显示 
  5. -- ============================================= 
  6. ALTER PROCEDURE [dbo].[category_showpage]  
  7.     @caid int, 
  8.     @startIndex int, 
  9.     @endIndex int 
  10. AS 
  11. BEGIN 
  12.     with temptable as ( 
  13.         select ROW_NUMBER() over (order by id desc) as 行号, * from 
  14.             ( 
  15.                 select n.id,n.titile,n.createTime,c.[name],n.caId from news n  
  16.                  inner join category c on n.caId =c.id and n.caId =@caid 
  17.             ) as aa 
  18.     )  
  19.  
  20.      
  21.     select * from temptable  where 行号 between @startIndex and @endIndex 
  22.      
  23. END 
-- =============================================
-- 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

运行后效果见下图:




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



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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值