Use ASP. NET to implement the paging Manager (advanced)

Source: Internet
Author: User

The automatic paging function is provided in the Web control of the DataGrid, but I have never used it, because the paging implemented by it is just a false

Phase. Why do we need paging? This is because there may be many qualified records. If you read all records at a time, not only

Data time, and memory is also a waste. The main purpose of paging is to solve these two problems (of course, it is not ruled out

Use pagination for the sake of beautiful UI ). But how does the web version of DataGrid implement paging? It does not intend to solve the above two problems

But read all the data at a time, and then display it in pages. This is a huge damage to efficiency and memory!
So I implemented the page manager ipaginationmanager, which reads the specified

And can cache a specified number of pages. The main features of this paging manager are:
(1) supports random jump. This is achieved through nested select statements.
(2) supports caching. Supported by enterpriseserverbase. Datastructure. fixcacher.
Let's take a 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 you select all columns, fields can pass null

Datatable getpage (INT index); // retrieves the index page
Datatable currentpage ();
Datatable prepage ();
Datatable nextpage ();

Int pagecount {Get ;}
Int cachersize {Get; set ;}
}
In this interface definition, the most important thing is the getpage () method. This method is implemented. The other three methods for obtaining pages

Currentpage, prepage, and nextpage are also very easy. In addition, the cachersize attribute allows us to specify the number of cached pages.

Quantity. If no cache is required, set its value to <= 0. If you need unlimited cache, the value is int. maxvalue.
The second initialize method in the ipaginationmanager interface, which is the data generated by xcodefactory.

The layer is used. Let's take a look at the definition of the parameter type datapaginationparas of the first initialize method:

Public class datapaginationparas
{
Public int pagesize = 10;
Public String [] fields = {"*"}; // The column to be searched. "*" indicates all columns.

Public String connectstring;
Public String tablename;
Public String wherestr; // where clause of the Search Condition

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 for the columns to be searched to be embedded into the SQL statement.

Other fields in datapaginationparas have obvious meanings.

Now let's take a look at the implementation of the paging Manager:

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 = NULL;
Private string fieldstrs = "";

///
/// Cachesize less than or equal to 0 -- indicates no cache, Int. maxvalue -- cache 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 Member
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 retrieves 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, 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
}
To understand the implementation of this class, you can start with the getpage (INT index) method, and the private method construtselectstr ()

This section describes how to use nested SQL statements for random paging search.
Finally, for the paging manager, it should be noted that the corresponding table to be searched must have a primary key named "ID"-this is the only

. In addition, the low-level encapsulation of data access used by the paging manager is defined in the enterpriseserverbase class library.
Using the paging manager is simple. After adding the UI interface, you only need to bind the returned datatable to the 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.