Implementing the paging manager with ASP.net

Source: Internet
Author: User
Tags definition
Asp.net| paging provides automatic paging in the DataGrid Web version control, but I never used it, because the pagination it implements is just an illusion. Why do we need pagination? That's because qualifying records can be a lot of things, and if you read all the records at once, you're not only prolonging the time to get the data, but it's also extremely wasteful of memory. The main purpose of paging is to address both of these issues (and, of course, to use paging for the sake of the UI's aesthetic needs). And how does the Web page's DataGrid implement pagination? It is not intended to solve both of these problems, but to read all the data at once and then display it as a paging. This is a great damage to efficiency and memory!

So I implemented my own paging manager Ipaginationmanager, Ipaginationmanager each time a specified page is read from the database, and a specified number of pages can be cached. The main features of the paging manager are:

(1) Support random jump. This is done through a nested SELECT statement.

(2) caching is supported. Support through EnterpriseServerBase.DataStructure.FixCacher.

Let's look at the definition of the Ipaginationmanager interface:

public interface Ipaginationmanager
{
void Initialize (Datapaginationparas paras);
void Initialize (Idbaccesser accesser, int page_size, String wherestr, string[] fields);//If all columns are selected, fields can pass NULL

DataTable getpage (int index); Remove Index page
DataTable currentpage ();
DataTable Prepage ();
DataTable NextPage ();

int pagecount{get;}
int cachersize{get; set;}
}
This interface definition, the most important is the GetPage () method, implemented this method, the other three ways to get the page currentpage, Prepage, NextPage is also very easy. In addition, the Cachersize property allows us to specify the number of cached pages. If caching is not required, set its value <=0, and if an infinite cache is required, the value is Int.maxvalue.

Ipaginationmanager interface in the second initialize method, you do not care, it is to the xcodefactory generated by the data layer used, Let's take a look at the definition of the parameter type Datapaginationparas for the first Initialize method:

public class Datapaginationparas
{
public int PageSize = 10;
Public string[] Fields = {"*"}; The column to search for, "*" means all columns

public string connectstring;
public string TableName;
public string wherestr; Where words for search criteria

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 is used to form a string of columns to be searched for embedding into an SQL statement. The meanings of the other fields in the Datapaginationparas are obvious.

Now let's look at the paging manager implementation:

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 is less than or equal to 0--Indicates no caching, int.maxvalue--caches all
///
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 Members
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 to remove the specified page
///
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, thi S.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.the PARAS.WHERESTR);
String outerselstr = String. Format ("Select top {0} * out ({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就可以了。


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.