查看: 6967|回复: 0
打印 上一主题 下一主题

实例分享:自己开发的自定义分页控件

[复制链接]
跳转到指定楼层
1#
发表于 2007-9-21 11:35:48 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
台州网址导航
一直以来都觉得分页是麻烦的事情,.NET中虽然有DATAGRID的分页,但用其他形式的列表仍然需要使用到分页,一次一次的写不利于效率及面向对象的方法,用类或用户控件也总觉得怪怪的,用第3方的自己觉得不放心,也不利于自己进行修改,干脆就自己写了一个。

  (另外注意:在控件编译时,可以在AssemblyInfo.cs文件中设置控件的标签和名称空间,如:

  第一个参数是名称空间(必须是你的控件类的名称空间),第二个是标签名(可自定义)

  记得要加入System.Web.UI;名称空间,另外 将控件类内的
  1.   
  2. [DefaultProperty("Text"),  
  3.      ToolboxData("")]这句屏蔽掉
复制代码
所有代码如下:
  1. using System;
  2. using System.Web.UI;
  3. using System.Web.UI.WebControls;
  4. using System.ComponentModel;
  5. using System.Collections;
  6. using System.Collections.Specialized;



  7. namespace PublicControls
  8. {
  9.      ///
  10.      /// PageNavigation 分页导航控件。
  11.      ///
  12.      [DefaultProperty("Text"),  
  13.      ToolboxData("")]
  14.      public class PageNavigation : Control,IPostBackDataHandler,IPostBackEventHandler  
  15.      {
  16.          #region预定义
  17.          
  18.          private string _style;
  19.          private int _count;
  20.          private int _pagesize;
  21.          private int _pageindex;
  22.         private int _pages;         //页群
  23.          private int _currentpages;
  24.          private string _first;
  25.          private string _pres;
  26.          private string _pre;
  27.          private string _next;
  28.          private string _nexts;
  29.          private string _last;


  30.          ///
  31.          ///委托页面索引事件
  32.          ///
  33.          public event EventHandler PageIndexChange;

  34.          #endregion

  35.          #region属性
  36.          
  37.          ///
  38.          ///相关属性样式字符串属性
  39.          ///
  40.          [Bindable(false),  
  41.          Category("StyleString"),  
  42.          DefaultValue("Align=left")]  
  43.          public string StyleString  
  44.          {
  45.               get
  46.               {
  47.                    _style = (string)ViewState["StyleString"];
  48.                    return _style;
  49.               }

  50.               set
  51.               {
  52.                    _style= value;
  53.                    ViewState["StyleString"] = _style;
  54.               }
  55.          }

  56.          ///
  57.          ///记录总数
  58.          ///
  59.          [Bindable(false),  
  60.          Category("Count"),  
  61.          DefaultValue(0)]  
  62.          public int Count  
  63.          {
  64.               get
  65.               {
  66.                    if (this.IsNumeric(ViewState["Count"]))
  67.                    {
  68.                        _count = (int)ViewState["Count"];
  69.                    }
  70.                    return _count;
  71.               }

  72.               set
  73.               {
  74.                    _count = value;
  75.                    ViewState["Count"] = _count;
  76.                     
  77.               }
  78.          }

  79.          ///
  80.          ///页面大小
  81.          ///
  82.          [Bindable(false),  
  83.          Category("Pagesize"),  
  84.          DefaultValue(10)]  
  85.          public int Pagesize
  86.          {
  87.               get
  88.               {
  89.                     
  90.                    if (this.IsNumeric(ViewState["Pagesize"]))
  91.                    {
  92.                        _pagesize = (int)ViewState["Pagesize"];
  93.                    }
  94.                    return _pagesize;
  95.               }

  96.               set
  97.               {
  98.                    _pagesize = value;
  99.                    ViewState["Pagesize"] = _pagesize;
  100.                     
  101.               }
  102.          }

  103.          ///
  104.          ///当前页索引
  105.          ///
  106.          [Bindable(false),  
  107.          Category("Pageindex"),  
  108.          DefaultValue(0)]  
  109.          public int Pageindex
  110.          {
  111.               get
  112.               {
  113.                    if (this.IsNumeric(ViewState["Pageindex"]))
  114.                    {
  115.                        _pageindex = (int)ViewState["Pageindex"];
  116.                    }
  117.                    return _pageindex;
  118.               }

  119.               set
  120.               {
  121.                    _pageindex = value;
  122.                    ViewState["Pageindex"] = _pageindex;
  123.               }
  124.          }

  125.          ///
  126.          ///页群属性
  127.          ///
  128.          public int Pages
  129.          {
  130.               get
  131.               {
  132.                    if (this.IsNumeric(ViewState["Pages"]))
  133.                    {
  134.                        _pages = (int)ViewState["Pages"];
  135.                    }
  136.                    return _pages;
  137.               }
  138.               set
  139.               {
  140.                    _pages = value;
  141.                    ViewState["Pages"] = _pages;
  142.               }
  143.          }

  144.          ///
  145.          ///当前页群索引
  146.          ///
  147.          public int CurrentPages
  148.          {
  149.               get
  150.               {
  151.                    if (this.IsNumeric(ViewState["CurrentPages"]))
  152.                    {
  153.                        _currentpages = (int)ViewState["CurrentPages"];
  154.                    }
  155.                    return _currentpages;
  156.               }
  157.               set
  158.               {
  159.                    _currentpages = value;
  160.                    ViewState["CurrentPages"] = _currentpages;
  161.               }
  162.          }

  163.          ///
  164.          ///标记样式
  165.          ///
  166.          [Bindable(false),  
  167.          Category("first"),  
  168.          DefaultValue("第一页")]  
  169.          public string First  
  170.          {
  171.               get
  172.               {
  173.                    _first = (string)ViewState["First"];
  174.                    return _first;
  175.               }

  176.               set
  177.               {
  178.                    _first = value;
  179.                    ViewState["First"] = _first;
  180.               }
  181.          }

  182.          ///
  183.          ///标记样式
  184.          ///
  185.          [Bindable(false),  
  186.          Category("pres"),  
  187.          DefaultValue("前N页")]  
  188.          public string Pres  
  189.          {
  190.               get
  191.               {
  192.                    _pres = (string)ViewState["Pres"];
  193.                    return _pres;
  194.               }

  195.               set
  196.               {
  197.                    _pres = value;
  198.                    ViewState["Pres"] = _pres;
  199.               }
  200.          }

  201.          ///
  202.          ///标记样式
  203.          ///
  204.          [Bindable(false),  
  205.          Category("pre"),  
  206.          DefaultValue("前一页")]  
  207.          public string Pre  
  208.          {
  209.               get
  210.               {
  211.                    _pre = (string)ViewState["Pre"];
  212.                    return _pre;
  213.               }

  214.               set
  215.               {
  216.                    _pre = value;
  217.                    ViewState["Pre"] = _pre;
  218.               }
  219.          }

  220.          ///
  221.          ///标记样式
  222.          ///
  223.          [Bindable(false),  
  224.          Category("next"),  
  225.          DefaultValue("后一页")]  
  226.          public string Next  
  227.          {
  228.               get
  229.               {
  230.                    _next = (string)ViewState["Next"];
  231.                    return _next;
  232.               }

  233.               set
  234.               {
  235.                    _next = value;
  236.                    ViewState["Next"] = _next;
  237.               }
  238.          }

  239.          ///
  240.          ///标记样式
  241.          ///
  242.          [Bindable(false),  
  243.          Category("nexts"),  
  244.          DefaultValue("后N页")]  
  245.          public string Nexts  
  246.          {
  247.               get
  248.               {
  249.                    _nexts = (string)ViewState["Nexts"];
  250.                    return _nexts;
  251.               }

  252.               set
  253.               {
  254.                    _nexts = value;
  255.                    ViewState["Nexts"] = _nexts;
  256.               }
  257.          }

  258.          ///
  259.          ///标记样式
  260.          ///
  261.          [Bindable(false),  
  262.          Category("last"),  
  263.          DefaultValue("最后一页")]  
  264.          public string Last  
  265.          {
  266.               get
  267.               {
  268.                    _last = (string)ViewState["Last"];
  269.                    return _last;
  270.               }
  271.               set
  272.               {
  273.                    _last = value;
  274.                    ViewState["Last"] = _last;
  275.               }
  276.          }


  277.          #endregion

  278.          #region事件
  279.          ///
  280.          ///当由类实现时,使服务器控件能够处理将窗体发送到服务器时引发的事件。
  281.          ///
  282.          ///所传递的参数
  283.          public void RaisePostBackEvent(string e)  
  284.          {
  285.               //当发生回送的时候改变控件当前索引
  286.               if(e=="top")
  287.               {
  288.                    Pageindex = 0;
  289.                    CurrentPages = 0 ;
  290.               }
  291.               else if(e=="last")
  292.               {
  293.                    Pageindex = (Count+Pagesize-1)/Pagesize -1;
  294.                     
  295.                    CurrentPages = ((Count+Pagesize-1)/Pagesize+Pages-1)/ Pages -1;

  296.                    if(Pageindex<0)
  297.                    {
  298.                        Pageindex=0;
  299.                    }
  300.                    if(CurrentPages<0)
  301.                    {
  302.                        CurrentPages=0;
  303.                    }
  304.                     
  305.               }
  306.               else if(e=="pre")
  307.               {
  308.                    if(Pageindex>0)
  309.                    {
  310.                        Pageindex--;
  311.                        if(Pageindex0)
  312.                        {
  313.                             CurrentPages--;
  314.                        }
  315.                    }
  316.               }
  317.               else if(e=="next")
  318.               {
  319.                    if(Pageindex<(Count+Pagesize-1)/Pagesize -1)
  320.                    {
  321.                        Pageindex++;
  322.                        if(Pageindex>(CurrentPages+1)*Pages-1 && Pageindex<Count-1)
  323.                        {
  324.                             CurrentPages++;
  325.                        }
  326.                    }
  327.               }
  328.               else if(e=="pres")
  329.               {
  330.                    Pageindex -= Pages;
  331.                    if(Pageindex<0)
  332.                    {
  333.                        Pageindex = 0;
  334.                    }
  335.                    if(Pageindex0)
  336.                    {
  337.                        CurrentPages--;
  338.                    }
  339.               }
  340.               else if(e=="nexts")
  341.               {
  342.                    Pageindex += Pages;
  343.                    if(Pageindex>(Count+Pagesize-1)/Pagesize -1)
  344.                    {
  345.                        Pageindex = (Count+Pagesize-1)/Pagesize -1;
  346.                    }
  347.                    if(Pageindex>(CurrentPages+1)*Pages-1 && Pageindex<Count-1)
  348.                    {
  349.                        CurrentPages++;
  350.                    }
  351.               }
  352.               else
  353.               {
  354.                    Pageindex = int.Parse(e.ToString());
  355.               }
  356.          
  357.               //发生回送事件时引发OnPageIndexChange事件
  358.               OnPageIndexChange(System.EventArgs.Empty);
  359.          }

  360.          ///
  361.          ///当由类实现时,为 ASP.NET 服务器控件处理回发数据。
  362.          ///
  363.          ///数据集合元素索引
  364.          ///string 的排序集合
  365.          ///
  366.          public bool LoadPostData(string postDataKey, NameValueCollection values)  
  367.          {
  368.               //Pageindex = Int32.Parse(values[this.UniqueID].Split('|')[0]);
  369.               //CurrentPages = Int32.Parse(values[this.UniqueID].Split('|')[1]);
  370.               return false;
  371.          }

  372.          ///
  373.          ///当由类实现时,用信号要求服务器控件对象通知 ASP.NET 应用程序该控件的状态已更改。
  374.          ///
  375.          public void RaisePostDataChangedEvent()  
  376.          {
  377.          }

  378.          ///
  379.          ///当页面索引改变,触发委托
  380.          ///
  381.          ///
  382.          protected void OnPageIndexChange(System.EventArgs e)
  383.          {     
  384.               //委托给加载控件页的PageIndexChange事件
  385.               if (PageIndexChange!=null)
  386.               {
  387.                    PageIndexChange(this,e);
  388.               }
  389.          }

  390.          ///
  391.          ///预呈现
  392.          ///
  393.          ///
  394.          protected override void OnPreRender(EventArgs e)  
  395.          {
  396.          }

  397.          #endregion

  398.          #region输出
  399.          ///
  400.          ///将此控件呈现给指定的输出参数。
  401.          ///
  402.          ///要写出到的 HTML 编写器  
  403.          protected override void Render(HtmlTextWriter output)
  404.          {

  405.               string Pagestring = "";  //定义页码呈现字符串
  406.               string Text = null;          //输出主结构字符串变量定义
  407.               int NO;

  408.               //输出主结构
  409.               Text = "";
  410.                
  411.               Text +=""+First+" ";

  412.               Text +=""+Pres+" ";

  413.               Text +=""+Pre+" ";

  414.               Text +="$Pagestring$";

  415.               Text +=""+Next+" ";

  416.               Text +=""+Nexts+" ";
  417.                
  418.               Text +=""+Last+" ";

  419.               Text +="";//";      

  420.               //当页大小小于0时,还原为1,防止预算出现除0错误
  421.               if (!(Pagesize>0))
  422.               {
  423.                    Pagesize = 1;
  424.               }
  425.                
  426.                

  427.               //计算出总页数,并循环输出页码
  428.               for (int i = 0;i< Pages ;i++ )
  429.               {
  430.                    //获得页群内页码
  431.                    NO = Pages * CurrentPages + i;

  432.                    if(NO<(Count+Pagesize-1)/Pagesize)
  433.                    {
  434.                        //判断页码是否为当前页
  435.                        if (Pageindex != NO)
  436.                        {
  437.                             Pagestring += ""+(NO+1).ToString()+" ";
  438.                        }
  439.                             //如果不是,页面部分无连接
  440.                        else
  441.                        {
  442.                             Pagestring += (NO+1).ToString()+" ";
  443.                        }
  444.                    }
  445.               }

  446.               if (!(Pagestring.Trim().Length>0))
  447.               {
  448.                    Pagestring = "1";
  449.               }
  450.                
  451.               //替换对齐属性
  452.               Text = Text.Replace("$style$",StyleString);
  453.             
  454.               //替换页码部分
  455.               Text = Text.Replace("$Pagestring$",Pagestring);
  456.                

  457.               output.Write(Text);
  458.                
  459.          }
  460.          #endregion

  461.          #region其他函数
  462.          ///
  463.          ///判断是否为数字字符串
  464.          ///
  465.          ///需验证的字符串
  466.          ///判断结果,符合条件为True,不符合条件为False
  467.          public bool IsNumeric(string str)
  468.          {
  469.               //判断是否为空
  470.               if (str == null || str.Length==0)  
  471.               {
  472.                    return false;  
  473.               }
  474.               //循环检查每个字符
  475.               foreach(char c in str)  
  476.               {  
  477.                    if (!Char.IsNumber(c))  
  478.                    {  
  479.                        return false;  
  480.                    }  
  481.               }  
  482.               return true;  
  483.          }

  484.          ///
  485.          ///判断是否为数字字符串
  486.          ///
  487.          ///需验证的字符串
  488.          ///判断结果,符合条件为True,不符合条件为False
  489.          public bool IsNumeric(object obj)
  490.          {

  491.               obj += "";
  492.               string str = obj.ToString();
  493.                
  494.               //判断是否为空
  495.               if (str == null || str.Length==0)  
  496.               {
  497.                    return false;  
  498.               }
  499.               //循环检查每个字符
  500.               foreach(char c in str)  
  501.               {  
  502.                    if (!Char.IsNumber(c))  
  503.                    {  
  504.                        return false;  
  505.                    }  
  506.               }  
  507.               return true;  
  508.          }

  509.          #endregion
  510.      }
  511. }
复制代码
  1. using System.Reflection;
  2. using System.Runtime.CompilerServices;
  3. using System.Web.UI;
  4. [assembly: TagPrefix("PublicControls","PublicControls")]  // 自定义控件前缀
复制代码
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播 分享分享 分享淘帖
台州维博网络(www.tzweb.com)专门运用PHP+MYSQL/ASP.NET+MSSQL技术开发网站门户平台系统等。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

网站推广
关于我们
  • 台州朗动科技(Tzweb.com)拥有多年开发网站平台系统门户手机客户端等业务的成功经验。主要从事:政企网站,系统平台,微信公众号,各类小程序,手机APP客户端,浙里办微应用,浙政钉微应用、主机域名、虚拟空间、后期维护等服务,满足不同企业公司的需求,是台州地区领先的网络技术服务商!

Hi,扫描关注我

Copyright © 2005-2026 站长论坛 All rights reserved

Powered by 站长论坛 with TZWEB Update Techonolgy Support

快速回复 返回顶部 返回列表