快精灵印艺坊 您身边的文印专家
广州名片 深圳名片 会员卡 贵宾卡 印刷 设计教程
产品展示 在线订购 会员中心 产品模板 设计指南 在线编辑
 首页 名片设计   CorelDRAW   Illustrator   AuotoCAD   Painter   其他软件   Photoshop   Fireworks   Flash  

 » 彩色名片
 » PVC卡
 » 彩色磁性卡
 » 彩页/画册
 » 个性印务
 » 彩色不干胶
 » 明信片
   » 明信片
   » 彩色书签
   » 门挂
 » 其他产品与服务
   » 创业锦囊
   » 办公用品
     » 信封、信纸
     » 便签纸、斜面纸砖
     » 无碳复印纸
   » 海报
   » 大篇幅印刷
     » KT板
     » 海报
     » 横幅

利用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 ;
   }

本新闻共2页,当前在第1页  1  2  

本新闻共2页,当前在第1页  1  2  


返回类别: 教程
上一教程: VS.NET RC5 EA版中所带Visio的UML能力测试报告
下一教程: ASP.NET 链接数据库基础

您可以阅读与"利用ASP.NET实现分页治理器"相关的教程:
· ASP.NET程序中用Repeater实现分页
· 在ASP.Net中两种利用CSS实现多界面的方式
· ASP.NET中利用存储过程实现模糊查询
· 用Asp.net实现基于XML的留言簿之一
· ASP.NET实现用户在线检测的类源码
    微笑服务 优质保证 索取样品