坏坏的分页控件
香水坏坏 发表于 2007-8-12 [ASP.NET]
控件特性:简单、灵活、性能优越
代码如下:
C#代码
- using System;
- using System.Web;
- using System.Web.UI;
- using System.Collections.Generic;
- using System.Text;
- namespace HooXin.Control
- {
- /// <summary>
- /// Summary description for Paging.
- /// </summary>
- [
- System.ComponentModel.Designer(typeof(PagingDesigner)),
- ToolboxData("<{0}:Paging runat=\"server\" />")
- ]
- public class Paging : System.Web.UI.Control
- {
- #region const
- protected const string VSKEY_ITEMCOUNT = "ItemCount";
- protected const string VSKEY_PAGEINDEX = "PageIndex";
- protected const string VSKEY_PAGESIZE = "PageSize";
- protected const string VSKEY_DISPLAYPAGES = "DisplayPages";
- protected const int PAGE_INDEX_DEFAULT = 1;
- protected const int PAGESIZE_DEFAULT = 20;
- protected const int DISPLAYPAGES_DEFAULT = 9;
- protected const string URLFORMAT_DEFAULT = "?page={0}";
- protected const string LINKFORMAT_DEFAULT = "<a href=\"{0}\" class=\"unsel\">{1}</a>";
- protected const string LINKFORMAT_ACTIVE_DEFAULT = "<a href=\"{0}\" class=\"sel\">{1}</a>";
- protected const string FIRSTFLAG_DEFAULT = "|<<";
- protected const string LASTFLAG_DEFAULT = ">>|";
- #endregion
- protected string _cssClass = "paging";
- protected string _firstFlag = FIRSTFLAG_DEFAULT;
- protected string _lastFlag = LASTFLAG_DEFAULT;
- protected string _urlFormat = URLFORMAT_DEFAULT;
- protected string _linkFormat = LINKFORMAT_DEFAULT;
- protected string _linkFormatActive = LINKFORMAT_ACTIVE_DEFAULT;
- protected int _padLeft;
- protected int _padRight;
- #region Accessors
- public string FirstFlag
- {
- get { return _firstFlag; }
- set { _firstFlag = value; }
- }
- public string LastFlag
- {
- get { return _lastFlag; }
- set { _lastFlag = value; }
- }
- public string CssClass
- {
- get { return _cssClass; }
- set { _cssClass = value; }
- }
- public int ItemCount
- {
- get
- {
- return (int)ViewState[VSKEY_ITEMCOUNT];
- }
- set
- {
- ViewState[VSKEY_ITEMCOUNT] = value;
- }
- }
- public int PageIndex
- {
- get
- {
- if ((int)ViewState[VSKEY_PAGEINDEX] <= MaxPages)
- return (int)ViewState[VSKEY_PAGEINDEX];
- else
- return 1;
- }
- set
- {
- ViewState[VSKEY_PAGEINDEX] = value;
- }
- }
- public int PageSize
- {
- get
- {
- return (int)ViewState[VSKEY_PAGESIZE];
- }
- set
- {
- ViewState[VSKEY_PAGESIZE] = value;
- }
- }
- public int DisplayPages
- {
- get
- {
- return (int)ViewState[VSKEY_DISPLAYPAGES];
- }
- set
- {
- ViewState[VSKEY_DISPLAYPAGES] = value;
- }
- }
- public int MaxPages
- {
- get
- {
- if (PageSize > 0)
- return (int)Math.Ceiling((double)ItemCount / PageSize);
- else
- return 0;
- }
- }
- public string UrlFormat
- {
- get { return _urlFormat; }
- set { _urlFormat = value; }
- }
- public string LinkFormat
- {
- get { return _linkFormat; }
- set { _linkFormat = value; }
- }
- public string LinkFormatActive
- {
- get { return _linkFormatActive; }
- set { _linkFormatActive = value; }
- }
- #endregion
- public Paging()
- {
- ViewState[VSKEY_ITEMCOUNT] = 0;
- ViewState[VSKEY_PAGEINDEX] = PAGE_INDEX_DEFAULT;
- ViewState[VSKEY_PAGESIZE] = PAGESIZE_DEFAULT;
- this.DisplayPages = DISPLAYPAGES_DEFAULT;
- }
- protected string RenderLink(int pageid, bool isCurrent)
- {
- return RenderLink(pageid, pageid.ToString(), isCurrent);
- }
- protected string RenderLink(int linkIndex, string display)
- {
- return RenderLink(linkIndex, display, false);
- }
- protected string RenderLink(int linkIndex, string display, bool isCurrent)
- {
- string url = String.Format(_urlFormat, linkIndex);
- return String.Format(isCurrent ? _linkFormatActive : _linkFormat,
- url, display);
- }
- protected void CalcPadding(int displayPages)
- {
- // want even padding if we can have it
- _padLeft = displayPages / 2;
- _padRight = _padLeft;
- if (displayPages % 2 == 0) _padLeft -= 1;
- }
- #region Render
- protected override void Render(HtmlTextWriter writer)
- {
- if (_cssClass.Length > 0)
- writer.AddAttribute("class", _cssClass);
- writer.RenderBeginTag(HtmlTextWriterTag.Div);
- //writer.Write(string.Format("<span class=\"tip\">共有记录{0}条,{1}条/页</span>", ItemCount, PageSize));
- if (ItemCount > 0)
- {
- writer.Write(RenderLink(1, FirstFlag, false));
- CalcPadding(DisplayPages);
- if ((PageIndex - _padLeft) <= 0)
- {
- // our current index falls inside the padded beginning: underflow
- _padRight += _padLeft - PageIndex + 1;
- _padLeft = PageIndex - 1;
- }
- else if ((PageIndex + _padRight) > MaxPages)
- {
- // our current index falls inside the padded end: overflow
- _padLeft += _padRight - (MaxPages - PageIndex);
- _padRight = MaxPages - PageIndex;
- }
- int counter = _padLeft;
- int idx = 1;
- while (counter > 0)
- {
- idx = PageIndex - counter;
- if (idx >= 1)
- break;
- counter--;
- }
- // starting at the place where we walked the counter back to, draw N links
- // as long as we're in the allowable bounds
- for (int i = idx; i < idx + DisplayPages; i++)
- {
- if (i >= 1 && i <= MaxPages)
- {
- writer.Write(RenderLink(i, i == PageIndex));
- }
- }
- // if we specified including 'Last' link back to the last page, write it plus
- // an optional spacer
- writer.Write(RenderLink(MaxPages, LastFlag, false));
- }
- writer.RenderEndTag();
- #if Diagnostic
- writer.Write("<br>PageIndex={0}, _padLeft={1}, _padRight={2}, MaxPages={3}, DisplayPages={4}, ItemCount={5}<br>",
- PageIndex, _padLeft, _padRight, MaxPages, DisplayPages, ItemCount);
- #endif
- }
- #endregion
- }
- public class PagingDesigner : System.Web.UI.Design.ControlDesigner
- {
- public override string GetDesignTimeHtml()
- {
- return base.GetDesignTimeHtml();
- }
- public override void Initialize(System.ComponentModel.IComponent component)
- {
- if (component is Paging)
- {
- Paging context = component as Paging;
- context.PageSize = 10;
- context.ItemCount = 120;
- context.PageIndex++;
- }
- base.Initialize(component);
- }
- }
- }
访客评论
发表评论
- 你的姓名:
- 你的网站:
- EMAIL:
- 评论内容:
- 私人

