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

📄 paging.cs

📁 微软的.NET论坛的源代码(COOL!!!)
💻 CS
📖 第 1 页 / 共 2 页
字号:
            td.Controls.Add(CreateCurrentPage());
            tr.Controls.Add(td);

            // Create page navigation
            td = new TableCell();
            td.HorizontalAlign = HorizontalAlign.Right;
            navigation = new Label();
            navigationText = new Label();
            navigationText.CssClass = "normalTextSmallBold";
            navigationText.Text = "Goto to page: ";

            navigation.Controls.Add(navigationText);
            
            // Numerical Paging
            navigation.Controls.Add(CreateNumericalNavigation());

            // Prev Next Paging
            navigation.Controls.Add(CreatePrevNextNavigation());

            td.Controls.Add(navigation);
            tr.Controls.Add(td);

            table.Controls.Add(tr);

            return table;
        }

        // *********************************************************************
        //  CreateCurrentPage
        //
        /// <summary>
        /// Display the page n of n+1 text
        /// </summary>
        /// 
        // ********************************************************************/ 
        private Control CreateCurrentPage() {
            currentPage = new Label();

            currentPage.CssClass = "normalTextSmallBold";

            return currentPage;
        }

        // *********************************************************************
        //  CreateNumericalNavigation
        //
        /// <summary>
        /// Creates numerical navigation link buttons
        /// </summary>
        /// 
        // ********************************************************************/ 
        private Control CreateNumericalNavigation() {
            numericalPaging = new PlaceHolder();
            int linkButtonsToCreate = 0;

            if (TotalPages > numericalLinkButtons.Length)
                linkButtonsToCreate = numericalLinkButtons.Length;
            else
                linkButtonsToCreate = TotalPages;

            // Create all the link buttons
            for (int i = 0; i < linkButtonsToCreate ; i++) {
                numericalLinkButtons[i] = new LinkButton();
                numericalLinkButtons[i].CssClass = "normalTextSmallBold";
                numericalLinkButtons[i].Click += new System.EventHandler(PageIndex_Click);
                numericalLinkButtons[i].Text = (i + 1).ToString("n0");
                numericalLinkButtons[i].CommandArgument = i.ToString();
                numericalPaging.Controls.Add(numericalLinkButtons[i]);
            }

            return numericalPaging;
        }

        // *********************************************************************
        //  CreatePrevNextNavigation
        //
        /// <summary>
        /// Creates previous/next navigation link buttons
        /// </summary>
        /// 
        // ********************************************************************/ 
        private Control CreatePrevNextNavigation() {
            PlaceHolder prevNext = new PlaceHolder();
            Label whitespace;

            // Create the previous button
            previousButton = new PlaceHolder();
            whitespace = new Label();
            whitespace.CssClass = "normalTextSmallBold";
            whitespace.Text = "&nbsp;";
            prev = new LinkButton();
            prev.CssClass = "normalTextSmallBold";
            prev.Text = "Prev";
            prev.ID = "Prev";
            prev.Click += new System.EventHandler(PrevNext_Click);
            previousButton.Controls.Add(whitespace);
            previousButton.Controls.Add(prev);
            prevNext.Controls.Add(previousButton);

            // Create the next button
            nextButton = new PlaceHolder();
            whitespace = new Label();
            whitespace.CssClass = "normalTextSmallBold";
            whitespace.Text = "&nbsp;";
            next = new LinkButton();
            next.CssClass = "normalTextSmallBold";
            next.Text = "Next";
            next.ID = "Next";
            next.Click += new System.EventHandler(PrevNext_Click);
            nextButton.Controls.Add(whitespace);
            nextButton.Controls.Add(next);
            prevNext.Controls.Add(nextButton);

            return prevNext;
        }
            
        // *********************************************************************
        //  PageIndex_Changed
        //
        /// <summary>
        /// Event raised when a an index has been selected by the end user
        /// </summary>
        /// 
        // ********************************************************************/
        public event System.EventHandler PageIndex_Changed;

        // *********************************************************************
        //  PageIndex_Click
        //
        /// <summary>
        /// Event raised when a new index is selected from the paging control
        /// </summary>
        /// 
        // ********************************************************************/
        private void PageIndex_Click(Object sender, EventArgs e) {

            PageIndex = Convert.ToInt32(((LinkButton) sender).CommandArgument);

            if (null != PageIndex_Changed)
                PageIndex_Changed(sender, e);

        }

        // *********************************************************************
        //  PrevNext_Click
        //
        /// <summary>
        /// Event raised when a new index is selected from the paging control
        /// </summary>
        /// 
        // ********************************************************************/
        private void PrevNext_Click(Object sender, EventArgs e) {

            PageIndex = Convert.ToInt32(((LinkButton) sender).CommandArgument);

            if (null != PageIndex_Changed)
                PageIndex_Changed(sender, e);

        }

        // *********************************************************************
        //  OnPreRender
        //
        /// <summary>
        /// Override OnPreRender and databind
        /// </summary>
        /// 
        // ********************************************************************/ 
        protected override void OnPreRender(EventArgs e) {

            // 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;
            
            // Control what gets displayed
            DisplayPager();
        }

        /// <summary>
        /// Specifies the Forum's posts you want to view.
        /// </summary>
        [
        Category("Required"),
        Description("Specifies the page size used in paging.")
        ]
        public int PageSize {
            get {
                if (pageSize == -1)
                    return Globals.PageSize;

                return pageSize;
            }
            set {
                pageSize = value;
            }
        }

        /// <summary>
        /// Specifies the Forum's posts you want to view.
        /// </summary>
        [
        Description("Specifies the current page in the index.")
        ]
        public int PageIndex {
            get {
                if (ViewState["PageIndex"] == null)
                    return pageIndex;

                return Convert.ToInt32(ViewState["PageIndex"]);
            }
            set {
                ViewState["PageIndex"] = value;
            }
        }

        /// <summary>
        /// Specifies the Forum's posts you want to view.
        /// </summary>
        private bool IsPagingEnabled {
            get {
                // the forumID is stuffed in the ViewState so that
                // it is persisted across postbacks.
                if (ViewState["pagingEnabled"] == null)
                    return isPagingEnabled;		// if it's not found in the ViewState, return the default value
					
                return Convert.ToBoolean(ViewState["pagingEnabled"].ToString());
            }
            set {
                // set the viewstate
                ViewState["pagingEnabled"] = value;
            }
        }

        /// <summary>
        /// Specifies the Forum's posts you want to view.
        /// </summary>
        public int TotalPages {
            get {
                return totalPages;
            }
            set {
                totalPages = value;
            }
        }

        /// <summary>
        /// Specifies the Forum's posts you want to view.
        /// </summary>
        public int TotalRecords {
            get {
                // the forumID is stuffed in the ViewState so that
                // it is persisted across postbacks.
                if (ViewState["totalRecords"] == null)
                    return defaultTotalRecords;		// if it's not found in the ViewState, return the default value
					
                return Convert.ToInt32(ViewState["totalRecords"].ToString());
            }
            set {
                TotalPages = CalculateTotalPages(value, PageSize);

                // set the viewstate
                ViewState["totalRecords"] = value;
            }
        }

        // *********************************************************************
        //  CalculateTotalPages
        //
        /// <summary>
        /// Static that caculates the total pages available.
        /// </summary>
        /// 
        // ********************************************************************/
        public static int CalculateTotalPages(int totalRecords, int pageSize) {
            int totalPagesAvailable;

            // First calculate the division
            totalPagesAvailable = totalRecords / pageSize;

            // Now do a mod for any remainder
            if ((totalRecords % pageSize) > 0)
                totalPagesAvailable++;

            return totalPagesAvailable;
        }
    }
}

⌨️ 快捷键说明

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