简体   繁体   中英

Custom Paging in a Grid View

I have a grid view in web user control and I'm dynamically adding the web user control inside an aspx page. I want custom pagination inside the grid so that initially it only loads the first 10 records so that the user control loads quickly, and then by clicking on "next" or page numbers it will load the next group of 10 records. How can this be achieved?

If You use Sqlserver's Stored Procedure then pass Two parameters which is Page Size and Page No.

In Page size it contains total number of record display and Page no contains Current Page.

In short you have to fetch only Page size record not all and call that stored procedure in grid's Page index changed Event.

this is a sample query to show how to fetch n records from query according to pagenumber

 create PROCEDURE [ProductCategorySearch]
    @PageIndex int = 1,
    @PageSize int = 10
    AS
    BEGIN

    DECLARE @StartRow int
    DECLARE @EndRow int

    SET @StartRow = (@PageSize * (@PageIndex - 1))  + 1  
    SET @EndRow = @PageSize * @PageIndex + 1

    SET NOCOUNT ON;

    WITH ArticleSearch AS
    (
        SELECT
           ROW_NUMBER() OVER 
             (
                -- Dynamic sorting
                ORDER BY 
                      a.pCategory_ID  ASC                

              ) AS RowNumber, 
                a.pCategory_ID ,a.pCategory_Name from     tblProductCategory a  

    )
    -- Statement that executes the CTE
    SELECT a.*,(select COUNT(*) from ArticleSearch) as RCount 
    FROM
          ArticleSearch a
    WHERE
          a.RowNumber BETWEEN @StartRow AND @EndRow - 1
    ORDER BY
          a.RowNumber

    END

您可以为此使用GridView.PageSize属性。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM