High-efficiency pagination with DataGrid custom paging function and stored procedure

xiaoxiao2021-03-06  49

ASP.NET uses DataGrid custom paging function and storage procedures to achieve efficient paging

CREATE Procedure ListProduct

(

@PageIndex Int, - Sequence number of page after paging

@PageSize Int, - A page size

@Conditionsql - SQL statement for query conditions

)

AS ... The specific code is not written (you can refer to the link above).

The specific SQL statement is as follows:

SELECT TOP 100 * from (Select * from Product Where ProductID <200000) T Where T.ProductId Not in

(Select Top 900 Productid WHERE PROMTID <200000) T1 ORDER BY T1.PRODUCTID ASC) Order by ProductID ASC

This statement takes the productID <200000 ($ 200,000) from the total commodity (300,000), then press the size pagination per page and then taken on page 10.

Public DataTable ListProduct (int pageIndex, int pageize)

{

//Ado.net is slightly ^ _ ^ from the code of the data from the database.

}

The data read by the above stored procedure is paised in DataGrid, and the dataGrid's allowpaging and allows are set to True.

Protected system.web.ui.webcontrols.datagrid productgrid;

ProductGrid.Allowpaging = true;

ProductGrid.AllowCustomPaging = true;

Then set the size of a page to display to display

ProductGrid.pageSize = 100; // According to the actual data, it is displayed.

Set up a page size, if you want to make DataGrid's number of pages, you must also set

ProductGrid.VirtualItemcount = getProductCount (); // getProductCount () function is to get the number of products that meet the condition, where the condition is ProductID <200000. After setting this property, then the number of pages of this DataGrid is

VirtualItemCount / PageSize, that is, the value of PageCount. You can't assign it directly because he is a read-only property.

These attributes are set to bind the data:

ProductGrid.DataSource = listuDuct (1, productgrid.pageSize); // In PageIndex in Page_Load, remember to determine IsPostback, execute these code when ISPostback is false

ProductGrid.databind ();

After such data binding, you can see a page with paging mode. But you can't really paginize. To achieve true paging, you must implement the following features.

Handle DataGrid's PageIndexchanged event (Events to handle a user new page)

Private void productgrid_pageIndexchanged (Object Source, System.Web.ui.WebControls.DataGridPageChangeDeventargs E)

{

/ / If you use 1 to represent the serial number of the first page in the stored procedure paging function, then you must use E.NewpageIndex 1 as PageIndex (if you select a page number 3 page number 3, then E.NewpageIndex is 2) Otherwise, use E.NewpageIndex directly.

ProductGrid.DataSource = listuDuct (E.NewpageIndex 1, ProductGrid.pagesize); // Read new data from the database from the database;

/ / Set the current PAGE number value, if it is not set, it will not become, which will cause misunderstandings to the user, as the data of all pages is the same.

ProductGrid.currentPageIndex = E.NewpageIndex;

}

If you handle the event of DataGrid Itemmand, you must add these code in front of the ItemMand event handling code:

IF (E.Item.itemType == ListItemType.pager)

{

Return;

}

Because when the pageindexchanged event is inspired, it is the user choice. If you don't have this, if you don't have this, you may have some unexpected situations (if you really need it, you can also do the above code, But it is best to test it.).

After the entire process is completed, browse the page again, feel that the speed is really much.

转载请注明原文地址:https://www.9cbs.com/read-115628.html

New Post(0)