Individual pager with repeater and SQL

I was looking for a good tutorial to teach me how to set up individual paging control with a simple DataBound control, such as Repeater, to implement a high-performance paging function.

I found many articles about this problem, but not one of them was the complete answer.

I use SQL, Item Repeater (with direct binding in the code, without using data sources), PageNumbers repeater (which will have a link as an ItemTemplate to transmit the request so that the method used can get the next part of the Elements), Label to hold the current page number and title.

N-Layered Web Applications ASP.NET 3.5 4: , ( ). SQL Data Access Lyaer, :

WITH Records AS ( SELECT ItemId, ItemName, ROW_NUMBER() OVER (ORDER BY ItemId) AS 'RowNumber' FROM   Items)  SELECT * FROM Records WHERE (RowNumber BETWEEN (@startIndex) AND @startIndex + @pageSize - 1)

, !

+3
1

pagination. :

    /// <summary>
    /// Produces html for a pagination control.
    /// </summary>
    /// <param name="page">Page number for the current page (1-based index).</param>
    /// <param name="pageSize">Number or items per page.</param>
    /// <param name="totalItems">Total number of items across all pages.</param>
    /// <returns>Html of a pagination control.</returns>
    public string RenderPaginationControl(int page, int pageSize, int totalItems)
    {
        int totalPages = (int)Math.Ceiling((double)totalItems/pageSize);

        // Create pager.
        StringBuilder pagerSb = new StringBuilder();
        for (int i = 1; i <= totalPages; ++i)
        {
            // If it is NOT a link to current page.
            if (i != page) { pagerSb.Append(string.Format("<a href='/data.aspx?page={0}'>{0}</a>", i)); }
            // If it is the link to current page.
            else { pagerSb.Append(string.Format("<span>{0}</span>", i)); }
        }

        return pagerSb.ToString();
    }

sql,

SELECT COUNT(*) FROM Items

totalItems RenderPaginationControl.

Repeater, :

this.MyRepeater.DataSource = DAL.GetItems(page, pageSize);
this.MyRepeater.DataBind();

int totalItems = DAL.GetTotalNumberOfItems();
this.PaginationLabel.Text = RenderPaginationControl(page, pageSize, totalItems);
+1

Source: https://habr.com/ru/post/1783101/


All Articles