KCMT Open Source Controls--easy and concise paging controls

Source: Internet
Author: User

KMCT name of the origin, this is my first blog on the control of the article, here I will make a statement to my control, the name of the control in my blog name of the first letter of the names (Keyboard,mouse,cigarette,tea) I hope everyone in the future to see my control When can think of my blog. At the same time, I chose open source, open source can promote the development of technology, but also to achieve the goal of resource sharing.

Less nonsense, directly into the theme, today's collation of pagination control, this control was born nearly a year, I and one of my colleagues to develop, is now widely used in the company's various projects. Control is developed based on. NET 2.0.

First, the macro analysis of the paging control: As a paging control should have the size of each page (Pagesize), the total number of pages (totalpage) and other attributes.

From the implementation that we have passed the parameter xxxx.aspx?page=1 like this in the previous paging process, we can only change the value of the page parameter to achieve the paging effect, so we just need to inherit the System.Web.UI.Control control and rewrite the render () method to implement this feature.

Post the Code:

Using System;

Using System.Text;

Using System.Text.RegularExpressions; Namespace KMCT. Control {///<summary>///This is a paging control, which is characterized by easy paging, lightweight///</summary> public class Pager:sys Tem.

        Web.UI.Control {private int _pagesize, _totalpage;

        private static readonly Regex RX;

        private string query;

        private int curretpage;

        private int mytotalpage;

            Filed public int Totalpage {get {return _totalpage;}

        set {_totalpage = value;}

            public int PageSize {get {_pagesize;}

        set {_pagesize = value;}

        The static Pager () {RX = new Regex (@ "^&page=\d+", regexoptions.compiled);}

            Public Pager () {query = Context.Request.Url.Query.Replace ("?", "&"); query = RX. Replace (query, String.

            Empty); String myPage = context.request["page"]; if (string.

            IsNullOrEmpty (myPage)) curretpage = 0; else {try {curretpage = Int.

                Parse (MyPage); catch {Context.Response.Redirect Regex.Replace (context.request .

                Url.tostring (), @ "page=[^&]+", "page=0"));

            }} protected override void Render (System.Web.UI.HtmlTextWriter writer) { if ((this._totalpage% this._pagesize) = = 0) {mytotalpage = This._totalpage/this._

            PageSize;

            else Mytotalpage = this._totalpage/this._pagesize + 1;

            String str = "<div class= ' Digg ' >";

            if (This._pagesize > this._totalpage) {str = ""; } else {if (This._totalpAge <= This._pagesize * {if (this.curretpage = 0)

                    STR + + "<span class= ' disabled ' > < </span>";

                    else str = "<a href= ' page=" + (curretPage-1) + this.query + "' > < </a>";  for (int i = 0; i < mytotalpage i++) {if curretpage

                        = = i) str + + "<span class= ' current ' >" + (i + 1) + "</span>"; else str = "<a href= '? page=" + i + this.query + "' >" + (i + 1) + "</a&gt

                    ;";                     } if (This.curretpage < myTotalPage-1) str = "<a href= ' page=" + (Curretpage + 1) + this

                    . Query + "' > > </a>"; else str = "<span class= ' disabled ' > > </span> ";} else {if (This.curretpage = = 0) str = "<span class= ' di

                    Sabled ' > < </span> ';

                    else str = "<a href= ' page=" + (curretPage-1) + this.query + "' > < </a>"; if (This.curretpage < 4) {for (int i = 0; i < 5; I + +) {if (curretpage = i) str = "

                            <span class= ' current ' > ' + (i + 1) + "</span>";

                        else str = "<a href= ' page=" + i + this.query + "' >" + (i + 1) + "</a>"; str + = "<a href= ' page=" + mytotalpage + this.query + "' >" + M

                    Ytotalpage + "</a>"; else if (thIs.curretpage >= 4 && this.curretpage < myTotalPage-6) {s

                        TR + + "<a href= ' page=1" + this.query + "' >1</a>";

                                for (int i =-2; i < 3; i++) {if (i = = 0)

                            STR + + "<span class= ' current ' >" + (this.curretpage + 1) + "</span>"; else str = "<a href= '? page=" + (This.curretpage + i) + this.query + "' >" + (th

                        Is.curretpage + 1 + i) + "</a>"; str + = "<a href= ' page=" + (myTotalPage-1) + this.query + "' >" + mytotalpage + "</

                    A> "; else if (Curretpage > mytotalpage + 1) {CONTEXT.R Esponse. Redirect (Regex.Replace (Context.Request.Url.ToString), @ "page=\d+", "page="+ (myTotalPage-1))); 

else {str = "<a href= '? page=1" + This.query +

                        "' >1</a>";

                                for (int i = 6; i > 0; i--) {if (curretpage = = mytotalpage-i)

                            STR + + "<span class= ' current ' >" + (mytotalpage-i + 1) + "</span>"; else str = "<a href= ' page=" + (mytotalpage-i) + This.que

                        ry + "' >" + (mytotalpage-i + 1) + "</a>"; } if (This.curretpage < myTotalPage-1) str = "&

                    Lt;a href= ' page= ' + (Curretpage + 1) + this.query + "' > > </a>";

                else str = "<span class= ' disabled ' > > </span>";

            }str + = "</div>"; Writer.

        Write (str); }     } }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.