Asp.net data paging method, asp.net data Paging

Source: Internet
Author: User

Asp.net data paging method, asp.net data Paging

/// <Summary> /// data paging method /// </summary> /// <param name = "PageIndex"> current page </param> /// <param name = "PageSize"> displayed quantity per page </param> // <param name = "PageCount"> total data </param> /// <param name =" url "> link, for example, list. aspx? Id = 1234 </param> // <returns> </returns> public static string GetPage (int PageIndex, int PageSize, int RecordCount, string Url) {StringBuilder sb = new StringBuilder (); try {// calculate the total number of pages int PageCount = RecordCount % PageSize = 0? RecordCount/PageSize: RecordCount/PageSize + 1; if (PageIndex <1) {PageIndex = 1;} if (PageIndex> PageCount) {PageIndex = PageCount;} string StarPage = ""; // homepage string EndPage = ""; // string PrePage = ""; // previous string NextPage = ""; // next page // if (PageIndex <= 1 | PageCount <= 1) {StarPage = ""; PrePage = "";} else {StarPage = ""; PrePage = "<li class = \" previous \ "> <a href = \"" + Url + "& page =" + (PageIndex-1) + "\"> previous page </a> </li> ";} // if (PageIndex = PageCount | PageCount <= 1) {EndPage = ""; NextPage = "" ;}else {EndPage = ""; nextPage = "<li class = \" next \ "> <a href = \" "+ Url +" & page = "+ (PageIndex + 1) + "\"> next page </a> </li> ";}// the page number output int PagerStart = 1; // The first page number if (PageCount> = 5) {PagerStart = PageIndex % 5 = 0? PageIndex-2: PageIndex-PageIndex % 5;} if (PagerStart <1) {PagerStart = 1;} string NumBtn = ""; for (int I = PagerStart; I <PagerStart + 5 & I <= PageCount; I ++) {if (I = PageIndex) {NumBtn + = "<li class = \" current \ "> <a>" + I + "</a> ";} else {NumBtn + = "<li> <a href = \" "+ Url +" & page = "+ I +" \ ">" + I + "</a> </li> ";}} sb. append (StarPage + PrePage + NumBtn + NextPage + EndPage);} catch {sb. append ("");} return sb. toString ();}

For details about the style, go to www.weixh.net.


How to add paging display to the aspnet page?

The core principle of the DataList paging method is to use the PagedDataSource object. The PagedDataSource class encapsulates the properties of the DataGrid control. These attributes enable the DataGrid to execute pagination. below is the public attribute of PagedDataSource:

AllowCustomPaging gets or sets the value indicating whether to enable custom paging.
AllowPaging gets or sets the value indicating whether to enable pagination.
Count to obtain the number of items to be used from the data source.
CurrentPageIndex gets or sets the index of the current page.
DataSource obtains or sets the data source.
Cececount gets the number of items in the data source.
FirstIndexInPage gets the first index on the page.
IsCustomPagingEnabled gets a value indicating whether to enable custom paging.
IsFirstPage gets a value indicating whether the current page is a homepage.
IsLastPage gets a value indicating whether the current page is the last page.
IsPagingEnabled gets a value indicating whether to enable paging.
IsReadOnly gets a value indicating whether the data source is read-only.
IsSynchronized gets a value indicating whether to synchronize access to the data source (thread safety ).
PageCount obtains the total number of pages required for all items in the data source.
PageSize gets or sets the number of items to be displayed on a single page.
VirtualCount gets or sets the actual number of items in the data source when custom pages are used.
So how to use the PagedDataSource object for DataList paging? (There are a lot of articles on the Internet that talk about DataList pages, some of which are confusing and some of which cannot work yet. I have been cheated once. This article is obtained through my personal practical experience, it can be executed smoothly. Hope to help you .)

Start pulling now!

Step 1: extract the data to the able, obtain the dataview, and pay it to the PagedDataSource object.

DataView objView = objTable. DefaultView;
PagedDataSource objPds = new PagedDataSource ();
ObjPds. DataSource = objView;
Step 2: configure the object objPds of PagedDataSource

ObjPds. AllowPaging = true;
ObjPds. PageSize = 4;
ObjPds. CurrentPageIndex = int. Parse (ViewState ["pageindex"]. ToString ());
The above two pieces of code are written in the BindData () function for each call when you click the paging button. The BindData () function is as follows:

Private void BindData ()
{
String SQL = "SELECT * From team ";

DataTable objTable = data. GetDataTable (SQL );
If (objTable! = Null & objTable. Rows. Count> 0)
{
DataView objView = objTable. DefaultView;
PagedDataSource objPds = new PagedDataSource ();
ObjPds. DataSource = objView;

Objpps. AllowPaging = true; ...... the remaining full text >>>

A newbie to ASPNet asked a question about the ability to extract data from the database and pagination.

First, select all records from the database for pagination of the GridView. Second, as long as the paging code is reasonable, it also exceeds the built-in GridView. At the same time, you use the Page type, added readability to facilitate separation of layers.
The general paging algorithm selects pagesize * pageindex first with TOP, and then uses TOP multiple times based on the ordered primary key until pagesize records are selected and returned.
If the page of the Forum is displayed, we recommend that you use REPEATER. You do not need to use the gridview because the additional functions will increase the overhead, and the repair or deletion function is not used in the list page of the Forum. at the same time, it is best to disable viewstat, if not. this is more effective and convenient than modifying the paging method.

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.