Customized Paging With Repeater And SQL

前端 未结 1 1686
时光取名叫无心
时光取名叫无心 2021-01-06 12:22

I\'ve been looking for a good tutorial to teach me how to make a customized Paging control with a simple DataBound control like Repeater to implement a high performance pagi

相关标签:
1条回答
  • 2021-01-06 12:52

    You can create a custom method to render your own pagination control. Here is an example:

        /// <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();
        }
    

    As you can see apart from your sql, you will also need to call

    SELECT COUNT(*) FROM Items
    

    and pass that value to totalItems in the RenderPaginationControl.

    And as far as the binding to Repeater is concerned - it's pretty straight forward:

    this.MyRepeater.DataSource = DAL.GetItems(page, pageSize);
    this.MyRepeater.DataBind();
    
    int totalItems = DAL.GetTotalNumberOfItems();
    this.PaginationLabel.Text = RenderPaginationControl(page, pageSize, totalItems);
    
    0 讨论(0)
提交回复
热议问题