All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
如何授予对存储过程的权限_[SQL Server教程]
C#如何把html中的相对路径变成绝对路径_[Asp.Net教程]
详解网页制作中使用的HTML常用标记_[Html教程]
在GDI+中绘制和克隆图像
PHP技巧:PHP程序中文件上传的安全问题_[PHP教程]
PHP中使用XML-RPC构造Web,Service简单入门_php资料_编程技术
学习笔记,ASP.NET,5种页面转向法_.net资料_编程技术
用PHP实现登陆验证码(类似条行码状)_[PHP教程]
在asp.net网页中实现tabPage功能(下)_[Asp.Net教程]
Asp.Net2.0中实现多任务异步页的一点提示_[Asp.Net教程]
Reading number is top 10 pictures
Embarrassing things comic collection2
Black and white also sexy--YanLiu1
美女和狗狗2
The money of more than 100 countries and regions16
人造器官和铁肺人
A man's favorite things12
The money of more than 100 countries and regions19
Household design comfortable contracted
Sell the barbecue as says father du breul2
The money of more than 100 countries and regions8
Download software ranking
C#高级编程(第4版)
Sora aoi, the maid, students' uniforms
Boxer's Top ten classic battle1
在线棋牌游戏3.05版
天龙八部十二宫服务端
WebService在.NET中的实战应用教学视频 → 第5集
Unix video tutorial13
Sora aoi 120 minutes
Proficient in JavaScript
软件工程思想
delv published in(发表于) 2014/1/16 9:31:04 Edit(编辑)
利用ASP.NET实现分页管理器(高级篇)_[Asp.Net教程]

利用ASP.NET实现分页管理器(高级篇)_[Asp.Net教程]

利用ASP.NET实现分页管理器(高级篇)_[Asp.Net教程]

在DataGrid的web版控件中提供了自动分页的功能,但是我从来没用过它,因为它实现的分页只是一种假相。我们为什么需要分页?那是因为符合条件的记录可能很多,如果一次读取所有的记录,不仅延长获取数据的时间,而且也极度浪费内存。而分页的存在的主要目的正是为了解决这两个问题(当然,也不排除为了UI美观的需要而使用分页的)。而web版的DataGrid是怎样实现分页的了?它并没有打算解决上述两个问题,而还是一次读取所有的数据,然后以分页的样子表现出来。这是对效率和内存的极大损害!


  于是我自己实现了分页管理器IPaginationManager ,IPaginationManager 每次从数据库中读取指定的任意一页,并且可以缓存指定数量的page。这个分页管理器的主要特点是:


  (1)支持随机跳转。这是通过嵌套Select语句实现的。


  (2)支持缓存。通过EnterpriseServerBase.DataStructure.FixCacher进行支持。


  先来看看IPaginationManager接口的定义:


public interface IPaginationManager
{
 void Initialize(DataPaginationParas paras) ;
 void Initialize(IDBAccesser accesser ,int page_Size ,string whereStr ,string[] fields) ;//如果选择所有列, fields可传null


 DataTable GetPage(int index) ; //取出第index页
 DataTable CurrentPage() ;
 DataTable PrePage() ;
 DataTable NextPage() ;


 int PageCount{get ;}
 int CacherSize{get; set; }
}
  这个接口定义中,最主要的是GetPage()方法,实现了这个方法,其它的三个获取页面的方法CurrentPage、PrePage、NextPage也就非常容易了。另外,CacherSize属性可以让我们指定缓存页面的数量。如果不需要缓存,则设置其值<=0,如果需要无限缓存,则值为Int.MaxValue。


  IPaginationManager接口中的第二个Initialize方法,你不要关心,它是给XCodeFactory生成的数据层使用了,我们来看看第一个Initialize方法的参数类型DataPaginationParas的定义:


public class DataPaginationParas
{
 public int PageSize = 10 ;
 public string[] Fields = {"*"}; //要搜索出的列,"*"表示所有列


 public string ConnectString ;
 public string TableName ;
 public string WhereStr ; //搜索条件的where字句


 public DataPaginationParas(string connStr ,string tableName ,string whereStr)
 {
  this.ConnectString = connStr ;
  this.TableName = tableName ;
  this.WhereStr = whereStr ;
 }


 #region GetFiedString
 public string GetFiedString()
 {
  if(this.Fields == null)
  {
   this.Fields = newstring[] {"*"} ;
  }


  string fieldStrs = "" ;


  for(int i=0 ;i  {
   fieldStrs += " " + this.Fields[i] ;
   if(i != (this.Fields.Length -1))
   {
    fieldStrs += " , " ;
   }
   else
   {
    fieldStrs += " " ;
   }
  }


  return fieldStrs ;
 }
 #endregion


}


  DataPaginationParas.GetFiedString用于把要搜索的列形成字符串以便嵌入到SQL语句中。DataPaginationParas中的其它字段的意思都很明显。


  现在来看看分页管理器的实现了:


public class PaginationManager :IPaginationManager
{
 private DataPaginationParas theParas ;
 private IADOBase adoBase ;
 private DataTable curPage = null ;
 private int itemCount = 0 ;
 private int pageCount = -1 ;
 private int curPageIndex = -1 ;


 private FixCacher fixCacher = null ;
 private string fieldStrs = "" ;


 ///
 /// cacheSize 小于等于0 -- 表示不缓存 ,Int.MaxValue -- 缓存所有
 ///
 public PaginationManager(int cacheSize)
 {
  if(cacheSize == int.MaxValue)
  {
   this.fixCacher = new FixCacher() ;
  }
  else if(cacheSize >0)
  {
   this.fixCacher = new FixCacher(cacheSize) ;
  }
  else
  {
   this.fixCacher = null ;
  }
 }


 public PaginationManager()
 {}


 #region IDataPaginationManager 成员
 public int CacherSize
 {
  get
  {
   if(this.fixCacher == null)
   {
    return 0 ;
   }
   return this.fixCacher.Size ;
  }
  set
  {
   if(this.fixCacher == null)
   {
    this.fixCacher = new FixCacher(value) ;
   }
   else
   {
    this.fixCacher.Size = value ;
   }
  }
 }
 public int PageCount
 {
  get
  {
   if(this.pageCount == -1)
   {
    string selCountStr = string.Format("Select count(*) from {0} {1}" ,this.theParas.TableName ,this.theParas.WhereStr) ;
    DataSet ds= this.adoBase.DoQuery(selCountStr) ;
    this.itemCount = int.Parse(ds.Tables[0].Rows[0][0].ToString()) ;
    this.pageCount = this.itemCount/this.theParas.PageSize ;
    if((this.itemCount%this.theParas.PageSize >0))
    {
     ++ this.pageCount ;
    }
   }
   return this.pageCount ;
  }
 }


 ///
 /// GetPage 取出指定的一页
 ///
 public DataTable GetPage(int index)
 {
  if(index == this.curPageIndex)
  {
   return this.curPage ;
  }


  if((index < 0) || (index >(this.PageCount-1)))
  {
   return null;
  }


  DataTable dt = this.GetCachedObject(index) ;


  if(dt == null)
  {
   string selectStr = this.ConstrutSelectStr(index) ;
   DataSet ds = this.adoBase.DoQuery(selectStr) ;
   dt = ds.Tables[0] ;


   this.CacheObject(index ,dt) ;
  }
  this.curPage = dt ;
  this.curPageIndex = index ;
  return this.curPage ;
 }


 private DataTable GetCachedObject(int index)
 {
  if(this.fixCacher == null)
  {
   return null ;
  }
  return (DataTable)this.fixCacher[index] ;
 }


 private void CacheObject(int index ,DataTable page)
 {
  if(this.fixCacher != null)
  {
   this.fixCacher.PutIn(index ,page) ;
  }
 }


 public DataTable CurrentPage()
 {
  return this.curPage ;
 }


 public DataTable PrePage()
 {
  return this.GetPage((--this.curPageIndex)) ;
 }


 public DataTable NextPage()
 {
  return this.GetPage((++this.curPageIndex)) ;
 }


 private string ConstrutSelectStr(int pageIndex)
 {
  if(pageIndex == 0)
  {
   return string.Format("Select top {0} {1} from {2} {3} ORDER BY ID" ,this.theParas.PageSize ,this.fieldStrs ,this.theParas.TableName ,this.theParas.WhereStr) ;
  }


  int innerCount = this.itemCount - this.theParas.PageSize*pageIndex ;
  string innerSelStr = string.Format("Select top {0} {1} from {2} {3} ORDER BY ID DESC " ,innerCount , this.fieldStrs ,this.theParas.TableName ,this.theParas.WhereStr) ;
  string outerSelStr = string.Format("Select top {0} * from ({1}) DERIVEDTBL ORDER BY ID" ,this.theParas.PageSize ,innerSelStr) ;


  return outerSelStr ;
 }


 #region Initialize
 public void Initialize(IDBAccesser accesser, int page_Size, string whereStr, string[] fields)
 {
  this.theParas = new DataPaginationParas(accesser.ConnectString ,accesser.DbTableName ,whereStr) ;
  this.theParas.Fields = fields ;
  this.theParas.PageSize = page_Size ;


  this.fieldStrs = this.theParas.GetFiedString() ;
  this.adoBase = new SqlADOBase(this.theParas.ConnectString) ;
 }


 public void Initialize(DataPaginationParas paras)
 {
  this.theParas = paras ;
  this.fieldStrs = this.theParas.GetFiedString() ;
  this.adoBase = new SqlADOBase(this.theParas.ConnectString) ;
 }


 #endregion
 #endregion
}
  了解这个类的实现,可以从GetPage(int index)方法入手,另外私有方法ConstrutSelectStr()的实现说明了如何使用嵌套sql语句进行随机分页搜索。


  最后,关于分页管理器,需要指出的是,搜索对应的表必须有一个名为"ID"的主键--这是唯一的要求。另外,分页管理器实现用到的数据访问低阶封装IADOBase定义于EnterpriseServerBase类库中。


  使用分页管理器是很简单的,加上UI界面后,只要把返回的DataTable绑定到DataGrid就可以了。


来源:网络







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.