坏坏的分页控件

香水坏坏 发表于 2007-8-12 [ASP.NET]

控件特性:简单、灵活、性能优越

分页存储过程点这里

代码如下:

 

C#代码
  1. using System;   
  2. using System.Web;   
  3. using System.Web.UI;   
  4. using System.Collections.Generic;   
  5. using System.Text;   
  6.   
  7. namespace HooXin.Control   
  8. {   
  9.     /// <summary>   
  10.     /// Summary description for Paging.   
  11.     /// </summary>   
  12.     [   
  13.     System.ComponentModel.Designer(typeof(PagingDesigner)),   
  14.     ToolboxData("<{0}:Paging runat=\"server\" />")   
  15.     ]   
  16.     public class Paging : System.Web.UI.Control   
  17.     {  
  18.         #region const   
  19.         protected const string VSKEY_ITEMCOUNT = "ItemCount";   
  20.         protected const string VSKEY_PAGEINDEX = "PageIndex";   
  21.         protected const string VSKEY_PAGESIZE = "PageSize";   
  22.         protected const string VSKEY_DISPLAYPAGES = "DisplayPages";   
  23.   
  24.         protected const int PAGE_INDEX_DEFAULT = 1;   
  25.         protected const int PAGESIZE_DEFAULT = 20;   
  26.         protected const int DISPLAYPAGES_DEFAULT = 9;   
  27.   
  28.         protected const string URLFORMAT_DEFAULT = "?page={0}";   
  29.         protected const string LINKFORMAT_DEFAULT = "<a href=\"{0}\" class=\"unsel\">{1}</a>";   
  30.         protected const string LINKFORMAT_ACTIVE_DEFAULT = "<a href=\"{0}\" class=\"sel\">{1}</a>";   
  31.   
  32.         protected const string FIRSTFLAG_DEFAULT = "|<<";   
  33.         protected const string LASTFLAG_DEFAULT = ">>|";  
  34.         #endregion   
  35.   
  36.         protected string _cssClass = "paging";   
  37.         protected string _firstFlag = FIRSTFLAG_DEFAULT;   
  38.         protected string _lastFlag = LASTFLAG_DEFAULT;   
  39.   
  40.         protected string _urlFormat = URLFORMAT_DEFAULT;   
  41.         protected string _linkFormat = LINKFORMAT_DEFAULT;   
  42.         protected string _linkFormatActive = LINKFORMAT_ACTIVE_DEFAULT;   
  43.         protected int _padLeft;   
  44.         protected int _padRight;  
  45.  
  46.         #region Accessors   
  47.   
  48.         public string FirstFlag   
  49.         {   
  50.             get { return _firstFlag; }   
  51.             set { _firstFlag = value; }   
  52.         }   
  53.   
  54.         public string LastFlag   
  55.         {   
  56.             get { return _lastFlag; }   
  57.             set { _lastFlag = value; }   
  58.         }   
  59.   
  60.         public string CssClass   
  61.         {   
  62.             get { return _cssClass; }   
  63.             set { _cssClass = value; }   
  64.         }   
  65.   
  66.         public int ItemCount   
  67.         {   
  68.             get  
  69.             {   
  70.                 return (int)ViewState[VSKEY_ITEMCOUNT];   
  71.             }   
  72.             set  
  73.             {   
  74.                 ViewState[VSKEY_ITEMCOUNT] = value;   
  75.             }   
  76.         }   
  77.   
  78.         public int PageIndex   
  79.         {   
  80.             get  
  81.             {   
  82.                 if ((int)ViewState[VSKEY_PAGEINDEX] <= MaxPages)   
  83.                     return (int)ViewState[VSKEY_PAGEINDEX];   
  84.                 else  
  85.                     return 1;   
  86.   
  87.             }   
  88.             set  
  89.             {   
  90.                 ViewState[VSKEY_PAGEINDEX] = value;   
  91.             }   
  92.         }   
  93.   
  94.         public int PageSize   
  95.         {   
  96.             get  
  97.             {   
  98.   
  99.                 return (int)ViewState[VSKEY_PAGESIZE];   
  100.   
  101.             }   
  102.             set  
  103.             {   
  104.                 ViewState[VSKEY_PAGESIZE] = value;   
  105.             }   
  106.         }   
  107.   
  108.         public int DisplayPages   
  109.         {   
  110.             get  
  111.             {   
  112.                 return (int)ViewState[VSKEY_DISPLAYPAGES];   
  113.             }   
  114.             set  
  115.             {   
  116.                 ViewState[VSKEY_DISPLAYPAGES] = value;   
  117.             }   
  118.         }   
  119.   
  120.         public int MaxPages   
  121.         {   
  122.             get  
  123.             {   
  124.                 if (PageSize > 0)   
  125.                     return (int)Math.Ceiling((double)ItemCount / PageSize);   
  126.                 else  
  127.                     return 0;   
  128.             }   
  129.         }   
  130.   
  131.   
  132.   
  133.   
  134.         public string UrlFormat   
  135.         {   
  136.             get { return _urlFormat; }   
  137.             set { _urlFormat = value; }   
  138.         }   
  139.   
  140.         public string LinkFormat   
  141.         {   
  142.             get { return _linkFormat; }   
  143.             set { _linkFormat = value; }   
  144.         }   
  145.   
  146.         public string LinkFormatActive   
  147.         {   
  148.             get { return _linkFormatActive; }   
  149.             set { _linkFormatActive = value; }   
  150.         }  
  151.  
  152.         #endregion   
  153.   
  154.         public Paging()   
  155.         {   
  156.             ViewState[VSKEY_ITEMCOUNT] = 0;   
  157.             ViewState[VSKEY_PAGEINDEX] = PAGE_INDEX_DEFAULT;   
  158.             ViewState[VSKEY_PAGESIZE] = PAGESIZE_DEFAULT;   
  159.             this.DisplayPages = DISPLAYPAGES_DEFAULT;   
  160.         }   
  161.   
  162.         protected string RenderLink(int pageid, bool isCurrent)   
  163.         {   
  164.             return RenderLink(pageid, pageid.ToString(), isCurrent);   
  165.         }   
  166.   
  167.         protected string RenderLink(int linkIndex, string display)   
  168.         {   
  169.             return RenderLink(linkIndex, display, false);   
  170.         }   
  171.   
  172.         protected string RenderLink(int linkIndex, string display, bool isCurrent)   
  173.         {   
  174.             string url = String.Format(_urlFormat, linkIndex);   
  175.             return String.Format(isCurrent ? _linkFormatActive : _linkFormat,   
  176.                 url, display);   
  177.         }   
  178.   
  179.   
  180.   
  181.         protected void CalcPadding(int displayPages)   
  182.         {   
  183.             // want even padding if we can have it   
  184.             _padLeft = displayPages / 2;   
  185.             _padRight = _padLeft;   
  186.   
  187.             if (displayPages % 2 == 0) _padLeft -= 1;   
  188.         }  
  189.  
  190.         #region Render   
  191.         protected override void Render(HtmlTextWriter writer)   
  192.         {   
  193.             if (_cssClass.Length > 0)   
  194.                 writer.AddAttribute("class", _cssClass);   
  195.   
  196.             writer.RenderBeginTag(HtmlTextWriterTag.Div);   
  197.             //writer.Write(string.Format("<span class=\"tip\">共有记录{0}条,{1}条/页</span>", ItemCount, PageSize));   
  198.             if (ItemCount > 0)   
  199.             {   
  200.                 writer.Write(RenderLink(1, FirstFlag, false));   
  201.   
  202.   
  203.                 CalcPadding(DisplayPages);   
  204.   
  205.                 if ((PageIndex - _padLeft) <= 0)   
  206.                 {   
  207.                     // our current index falls inside the padded beginning: underflow   
  208.                     _padRight += _padLeft - PageIndex + 1;   
  209.                     _padLeft = PageIndex - 1;   
  210.                 }   
  211.                 else if ((PageIndex + _padRight) > MaxPages)   
  212.                 {   
  213.                     // our current index falls inside the padded end: overflow   
  214.                     _padLeft += _padRight - (MaxPages - PageIndex);   
  215.                     _padRight = MaxPages - PageIndex;   
  216.                 }   
  217.   
  218.                 int counter = _padLeft;   
  219.                 int idx = 1;   
  220.                 while (counter > 0)   
  221.                 {   
  222.                     idx = PageIndex - counter;   
  223.                     if (idx >= 1)   
  224.                         break;   
  225.                     counter--;   
  226.                 }   
  227.   
  228.   
  229.                 // starting at the place where we walked the counter back to, draw N links   
  230.                 // as long as we're in the allowable bounds   
  231.                 for (int i = idx; i < idx + DisplayPages; i++)   
  232.                 {   
  233.                     if (i >= 1 && i <= MaxPages)   
  234.                     {   
  235.                         writer.Write(RenderLink(i, i == PageIndex));   
  236.   
  237.                     }   
  238.                 }   
  239.   
  240.                 // if we specified including 'Last' link back to the last page, write it plus    
  241.                 // an optional spacer   
  242.   
  243.   
  244.                 writer.Write(RenderLink(MaxPages, LastFlag, false));   
  245.             }   
  246.   
  247.             writer.RenderEndTag();  
  248.  
  249. #if Diagnostic   
  250.             writer.Write("<br>PageIndex={0}, _padLeft={1}, _padRight={2}, MaxPages={3}, DisplayPages={4}, ItemCount={5}<br>",    
  251.                 PageIndex, _padLeft, _padRight, MaxPages, DisplayPages, ItemCount);  
  252. #endif   
  253.   
  254.         }  
  255.         #endregion   
  256.     }   
  257.   
  258.   
  259.     public class PagingDesigner : System.Web.UI.Design.ControlDesigner   
  260.     {   
  261.         public override string GetDesignTimeHtml()   
  262.         {   
  263.             return base.GetDesignTimeHtml();   
  264.         }   
  265.   
  266.         public override void Initialize(System.ComponentModel.IComponent component)   
  267.         {   
  268.             if (component is Paging)   
  269.             {   
  270.                 Paging context = component as Paging;   
  271.                 context.PageSize = 10;   
  272.                 context.ItemCount = 120;   
  273.                 context.PageIndex++;   
  274.             }   
  275.             base.Initialize(component);   
  276.         }   
  277.   
  278.     }   
  279. }   
772 4 标签:C# 分页 
访客评论
  • 香水坏坏    [2007-11-24]
    Re:tsyung

    杀鸡焉用牛刀
  • tsyung    [2007-11-24]
    這個控件沒什么實用價值。只是通過url傳替參數分頁,沒有服務端邏輯,何必用ViewState?
    要做標準的asp.net服務器控件,建議先看看Microsoft Press的那本《Developing Microsoft ASP.NET Server Controls and Components》.
  • omeweb    [2007-9-30]
    你还用ViewState吗?如果禁用了ViewState,这个控件还有作用吗?其实完全没必要用ViewState
  • fx    [2007-8-13]
    强啊
发表评论
  • 你的姓名:
  • 你的网站:
  •   EMAIL:   
  • 评论内容:
  • 私人