ASP.NET分页正品—分页真



承接上篇博文《ASP.NET真假分页-假分页》:http://blog.csdn.net/u010773667/article/details/38845009,继续解说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控件。在右下角将会显现一个小button,单击打开,对导航button显示文本进行设置。



执行后效果:


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


改进效果见下图:


假设还想显示的更加详细,可进行自己定义信息区显示方式及内容设置



上述对控件进行的全部设置将在VS中自己主动生成对应代码,我们也能够通过手动输入代码进行设置,此处不介绍。


好了。设置好了前台。接下来就要进行数据绑定了(注意:在方法anpCa_PageChanged()中绑定了caid=6,此处须要又一次获得类别ID,我没有解决。。

希望会的朋友友情提示一下)


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层数据查询的部分代码展示

</pre></p><p></p><pre class="html" name="code">
</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


存储过程(非常重要)


-- =============================================
-- 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),在页索引动态变化的过程中须要一直又一次获得,这一点我没有实现,希望小伙伴们能够帮助我。谢谢!



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




posted @ 2015-12-08 10:25  phlsheji  阅读(302)  评论(0编辑  收藏  举报