⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 paging.cs

📁 微软的.NET论坛的源代码(COOL!!!)
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Drawing;
using System.Collections;
using System.Collections.Specialized;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AspNetForums;
using AspNetForums.Components;
using System.ComponentModel;
using System.IO;

namespace AspNetForums.Controls {


    // *********************************************************************
    //  Paging
    //
    /// <summary>
    /// The paging control is used to display paging options for controls that
    /// page through data. This control is necessary since some data rendering 
    /// controls, such as the DataList, do not support paging and it must be 
    /// a custom implementation.
    /// </summary>
    /// 
    // ********************************************************************/
    public class Paging : WebControl, INamingContainer {

        // Paging
        int pageIndex = 0;
        int totalPages = 0;
        const int defaultTotalRecords = 0;
        const bool isPagingEnabled = false;
        int pageSize = -1;

        // Controls
        Label currentPage;
        PlaceHolder previousButton;
        PlaceHolder nextButton;
        LinkButton prev;
        LinkButton next;
        LinkButton[] numericalLinkButtons;
        PlaceHolder numericalPaging;

        // *********************************************************************
        //  Paging
        //
        /// <summary>
        /// Constructor
        /// </summary>
        /// 
        // ********************************************************************/ 
        public Paging() {

            // Anything we want on the query string?
            if (null != Context)
                if (null != Context.Request.QueryString["PageIndex"]) {
                    pageIndex = (Convert.ToInt32(Context.Request.QueryString["PageIndex"]) - 1);
                }

        }

        // *********************************************************************
        //  CreateChildControls
        //
        /// <summary>
        /// This event handler adds the children controls and is resonsible
        /// for determining the template type used for the control.
        /// </summary>
        /// 
        // ********************************************************************/ 
        protected override void CreateChildControls() {

            // If the total number of records is less than the
            // number of records we display in a page, we'll simply
            // return.
            if (TotalRecords <= PageSize)
                return;

            // Quick check to ensure the PageIndex is not greater than the Page Size
            if ((PageIndex > PageSize) || (PageIndex < 0))
                PageIndex = 0;

            // How many link buttons do we need?
            numericalLinkButtons = new LinkButton[TotalPages];

            // Add the control to display navigation
            Controls.Add(NavigationDisplay());

        }

        // *********************************************************************
        //  DisplayPager
        //
        /// <summary>
        /// Used to display the pager. Is public so that the parent control can 
        /// reset the pager when a post back occurs that the pager did not raise.
        /// </summary>
        /// 
        // ********************************************************************/ 
        private void DisplayPager() {
            DisplayCurrentPage();
            DisplayNumericalPaging();
            DisplayPrevNext();
        }

        // *********************************************************************
        //  DisplayCurrentPage
        //
        /// <summary>
        /// Displays the current page that the user is viewing
        /// </summary>
        /// 
        // ********************************************************************/ 
        private void DisplayCurrentPage() {
            currentPage.Text = "Page " + (PageIndex + 1 ).ToString("n0") + " of " + TotalPages.ToString("n0");
        }


        // *********************************************************************
        //  DisplayNumericalPaging
        //
        /// <summary>
        /// Controls how the numerical link buttons get rendered
        /// </summary>
        /// 
        // ********************************************************************/ 
        private void DisplayNumericalPaging() {
            int itemsToDisplay = 3;
            int lowerBoundPosition = 1;
            int upperBoundPosition = (TotalPages - 1);
            Label label;

            // Clear out the controls
            numericalPaging.Controls.Clear();
            
            // If we have less than 6 items we don't need the fancier paging display
            if ((upperBoundPosition + 1) < 6) {
                for (int i = 0; i < (upperBoundPosition + 1); i++) {

                    // Don't display a link button for the existing page
                    if (i == PageIndex) {
                        label = new Label();
                        label.CssClass = "normalTextSmallBold";
                        label.Text = "[" + (PageIndex + 1).ToString("n0") + "]";
                        numericalPaging.Controls.Add(label);
                    } else {
                        numericalPaging.Controls.Add(numericalLinkButtons[i]);
                    }

                    if (i + 1 != numericalLinkButtons.Length) {
                        label = new Label();
                        label.CssClass = "normalTextSmallBold";
                        label.Text = ", ";

                        numericalPaging.Controls.Add(label);
                    }
                }            

                return;
            }
            
            // Always display the first 3 if available
            if (numericalLinkButtons.Length < itemsToDisplay)
                itemsToDisplay = numericalLinkButtons.Length;

            for (int i = 0; i < itemsToDisplay; i++) {
                numericalPaging.Controls.Add(numericalLinkButtons[i]);

                if (i + 1 != itemsToDisplay) {
                    label = new Label();
                    label.CssClass = "normalTextSmallBold";
                    label.Text = ", ";

                    numericalPaging.Controls.Add(label);
                }
            }

            // Handle the lower end first
            if ((PageIndex - lowerBoundPosition) <= (upperBoundPosition - PageIndex) ) {

                for (int i = 3; i < PageIndex + 2; i++) {
                    label = new Label();
                    label.CssClass = "normalTextSmallBold";
                    label.Text = ", ";

                    numericalPaging.Controls.Add(label);
                    numericalPaging.Controls.Add(numericalLinkButtons[i]);
                }
            }

            // Insert the ellipses or a trailing comma if necessary
            label = new Label();
            label.CssClass = "normalTextSmallBold";
            if (upperBoundPosition == 3) {
                label.Text = ", ";
            } else if (upperBoundPosition >= 4) {
                label = new Label();
                label.CssClass = "normalTextSmallBold";
                label.Text = " ... ";
            }
            numericalPaging.Controls.Add(label);

            // Handle the upper end
            if ((PageIndex - lowerBoundPosition) > (upperBoundPosition - PageIndex) ) {

                    for (int i = PageIndex - 1; i < upperBoundPosition; i++) {
                        label = new Label();
                        label.CssClass = "normalTextSmallBold";
                        label.Text = ", ";

                        if (i > PageIndex -1)
                            numericalPaging.Controls.Add(label);

                        numericalPaging.Controls.Add(numericalLinkButtons[i]);
                    }

            }

            // Always display the last 2 if available
            if ((numericalLinkButtons.Length > 3) && (TotalPages > 5)) {
                itemsToDisplay = 2;

                for (int i = itemsToDisplay; i > 0 ; i--) {
                    numericalPaging.Controls.Add(numericalLinkButtons[(upperBoundPosition + 1) - i]);

                    if (i + 1 != itemsToDisplay) {
                        Label tmp = new Label();
                        tmp.CssClass = "normalTextSmallBold";
                        tmp.Text = ", ";
                        numericalPaging.Controls.Add( tmp );
                    }
                }
            }
        }      

        // *********************************************************************
        //  DisplayPrevNext
        //
        /// <summary>
        /// Controls how the previous next link buttons get rendered
        /// </summary>
        /// 
        // ********************************************************************/ 
        private void DisplayPrevNext() {
            prev.CommandArgument = (PageIndex - 1).ToString();
            next.CommandArgument = (PageIndex + 1).ToString();

            // Control what gets displayed
            if ((PageIndex > 0) && ((PageIndex + 1) < TotalPages)) {
                nextButton.Visible = true;
                previousButton.Visible = true;
            } else if (PageIndex == 0) {
                nextButton.Visible = true;
                previousButton.Visible = false;
            } else if ((PageIndex + 1) == TotalPages) {
                nextButton.Visible = false;
                previousButton.Visible = true;
            }
        }        

        // *********************************************************************
        //  NavigationDisplay
        //
        /// <summary>
        /// Control that contains all the navigation display details.
        /// </summary>
        /// 
        // ********************************************************************/ 
        private Control NavigationDisplay() {

            Table table;
            TableRow tr;
            TableCell td;
            Label navigation;
            Label navigationText;

            // Create a new table
            table = new Table();
            table.CellPadding = 0;
            table.CellSpacing = 0;
            table.Width = Unit.Percentage(100);

            // We only have a single row
            tr = new TableRow();

            // Two columns. One for the current page and one for navigation
            td = new TableCell();

            // Display the current page

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -