KMCT名字的由來,這是我在Blog中的第一篇關於控件的文章,在此我將對我的控件做一個申明,控件 名字以我的Blog名字各項的首字母命名(Keyboard,Mouse,Cigarette,Tea )希望大家在以後看我的控件的 時候能想到我的Blog。同時我選擇了開源,開源更能促進技術的發展,也更能到達資源共享的目的。
廢話少說、直接進入主題,今天整理的是分頁控件,這個控件誕生將近一年了,是我和我的一個同事 共同開發,現在被廣泛用於公司的各個項目中。控件基於.net 2.0 開發。
先從宏觀上分析一下分頁控件:作為分頁控件應該具有每一頁的大小(Pagesize)、總頁數 (TotalPage)等屬性。
從實現上說我們在以前的分頁過程中都是像這樣傳遞參數xxxx.aspx?page=1,那麼我們只要改變page 參數的值便可以實現分頁的效果,因此我們只需要繼承System.Web.UI.Control控件並重寫Render()方法 即可實現該功能。
貼出代碼:
using System; using System.Text; using System.Text.RegularExpressions; namespace KMCT.Control { /// <summary> /// 該控件為分頁控件,其特點是分頁方便,輕量級 /// </summary> public class Pager : System.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 { return _pageSize; } set { _pageSize = value; } } 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 * 10) { 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>"; } 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='disabled'> < </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 + "'>" + myTotalPage + "</a>"; } else if (this.curretPage >= 4 && this.curretPage < myTotalPage - 6) { str += "<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 + "'>" + (this.curretPage + 1 + i) + "</a>"; } str += "<a href='?page=" + (myTotalPage - 1) + this.query + "'>" + myTotalPage + "</a>"; } else if (curretPage > myTotalPage + 1) { Context.Response.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.query + "'>" + (myTotalPage - i + 1) + "</a>"; } } if (this.curretPage < myTotalPage - 1) str += "<a href='?page=" + (curretPage + 1) + this.query + "'> > </a>"; else str += "<span class='disabled'> > </span>"; } } str += "</div>"; writer.Write(str); } } }