小巧精致的ASP.Net分页控件

  1. using System;   
  2.   
  3. using System.Data;   
  4.   
  5. using System.Configuration;   
  6.   
  7. using System.Collections;   
  8.   
  9. using System.Web;   
  10.   
  11. using System.Web.Security;   
  12.   
  13. using System.Web.UI;   
  14.   
  15. using System.Web.UI.WebControls;   
  16.   
  17. using System.Web.UI.WebControls.WebParts;   
  18.   
  19. using System.Web.UI.HtmlControls;   
  20.   
  21. using System.Text;   
  22.   
  23. using System.Text.RegularExpressions;   
  24.   
  25.   
  26.   
  27. namespace MyTest    
  28.   
  29. {   
  30.   
  31.     public partial class PageShow : System.Web.UI.UserControl   
  32.   
  33.     {  
  34.  
  35.         #region 私有成员   
  36.   
  37.         /// <summary>   
  38.   
  39.         /// 当前页的页码的背景色   
  40.   
  41.         /// </summary>   
  42.   
  43.         private string _color = "#a8ceff";   
  44.   
  45.   
  46.   
  47.         /// <summary>   
  48.   
  49.         /// 总记录数   
  50.   
  51.         /// </summary>   
  52.   
  53.         private int _recTotal = 0;   
  54.   
  55.   
  56.   
  57.         /// <summary>   
  58.   
  59.         /// 每页显示的记录数, 默认为10   
  60.   
  61.         /// </summary>   
  62.   
  63.         private int _pageSize = 10;   
  64.   
  65.   
  66.   
  67.         /// <summary>   
  68.   
  69.         /// 当前页的页码左右要显示的页码的个数, 默认7个   
  70.   
  71.         /// </summary>   
  72.   
  73.         private int _pageShowNum = 7;  
  74.  
  75.         #endregion  
  76.  
  77.  
  78.  
  79.         #region 公共属性   
  80.   
  81.         /// <summary>   
  82.   
  83.         /// 设置或读取总记录数,不能小于0   
  84.   
  85.         /// </summary>   
  86.   
  87.         public int RecTotal   
  88.   
  89.         {   
  90.   
  91.             get { return _recTotal; }   
  92.   
  93.             set  
  94.   
  95.             {   
  96.   
  97.                 if (value > 0)    
  98.   
  99.                     _recTotal = value;   
  100.   
  101.             }   
  102.   
  103.         }   
  104.   
  105.   
  106.   
  107.         /// <summary>   
  108.   
  109.         /// 设置或读取每页显示的记录数,不能小于0   
  110.   
  111.         /// </summary>   
  112.   
  113.         public int PageSize   
  114.   
  115.         {   
  116.   
  117.             get { return _pageSize; }   
  118.   
  119.             set  
  120.   
  121.             {   
  122.   
  123.                 if (value > 0)    
  124.   
  125.                     _pageSize = value;   
  126.   
  127.             }   
  128.   
  129.         }   
  130.   
  131.   
  132.   
  133.         /// <summary>   
  134.   
  135.         /// 读取总页数   
  136.   
  137.         /// </summary>   
  138.   
  139.         public int PageTotal   
  140.   
  141.         {   
  142.   
  143.             get {   
  144.   
  145.                 int total = (int)Math.Ceiling((double)_recTotal / _pageSize);   
  146.   
  147.                 return total;   
  148.   
  149.                 //return PageTotal;    
  150.   
  151.             }   
  152.   
  153.         }   
  154.   
  155.   
  156.   
  157.         /// <summary>   
  158.   
  159.         /// 读取当前要显示第几页,注:页码传递方式是Request.QueryString["page"]   
  160.   
  161.         /// </summary>   
  162.   
  163.         public int PageIndex   
  164.   
  165.         {   
  166.   
  167.             get  
  168.   
  169.             {   
  170.   
  171.                 int pageIndex = 1;   
  172.   
  173.   
  174.   
  175.                 string tmp = Request.QueryString["page"];   
  176.   
  177.                 if (tmp != null && tmp != string.Empty)   
  178.   
  179.                 {   
  180.   
  181.                     if (tmp == "end")   
  182.   
  183.                         pageIndex = PageTotal;   
  184.   
  185.                     else  
  186.   
  187.                     {   
  188.   
  189.                         try  
  190.   
  191.                         {   
  192.   
  193.                             pageIndex = int.Parse(tmp);   
  194.   
  195.                             if (pageIndex > PageTotal)   
  196.   
  197.                                 pageIndex = PageTotal;   
  198.   
  199.                             else if (pageIndex < 1)   
  200.   
  201.                                 pageIndex = 1;   
  202.   
  203.                         }   
  204.   
  205.                         catch (Exception)   
  206.   
  207.                         {   
  208.   
  209.                             pageIndex = 1;   
  210.   
  211.                         }   
  212.   
  213.                     }   
  214.   
  215.                 }   
  216.   
  217.                 return pageIndex;   
  218.   
  219.             }   
  220.   
  221.         }   
  222.   
  223.   
  224.   
  225.         /// <summary>   
  226.   
  227.         /// 设置或读取当前页的页码的背景色,默认为#a8ceff   
  228.   
  229.         /// </summary>   
  230.   
  231.         public string Color   
  232.   
  233.         {   
  234.   
  235.             get { return _color; }   
  236.   
  237.             set { _color = value; }   
  238.   
  239.         }   
  240.   
  241.   
  242.   
  243.         /// <summary>   
  244.   
  245.         /// 当前页的页码左右要显示的页码的个数,默认7个   
  246.   
  247.         /// </summary>   
  248.   
  249.         public int PageShowNum   
  250.   
  251.         {   
  252.   
  253.             get { return _pageShowNum; }   
  254.   
  255.             set { _pageShowNum = value; }   
  256.   
  257.         }  
  258.  
  259.         #endregion   
  260.   
  261.   
  262.   
  263.         /// <summary>   
  264.   
  265.         /// 重写Render事件,发送生成的分页Url   
  266.   
  267.         /// </summary>   
  268.   
  269.         /// <param name="writer"></param>   
  270.   
  271.         protected override void Render(HtmlTextWriter writer)   
  272.   
  273.         {   
  274.   
  275.             if (PageTotal <= 1)   
  276.   
  277.             {//少于1页时,不显示   
  278.   
  279.                 return;   
  280.   
  281.             }   
  282.   
  283.   
  284.   
  285.             StringBuilder writeStr = new StringBuilder();  
  286.  
  287.  
  288.  
  289.             #region 控件使用的CSS代码   
  290.   
  291.             writeStr.Append(@"   
  292.   
  293. <style type='text/css'>.page_total {background-color: #F5FBFF; border: 1px solid #86B9D6; border-right: 0px solid #86B9D6; font-weight: bold; }   
  294.   
  295. .page_pages {background-color: #F5FBFF; border: 1px solid #86B9D6; margin-right:1px; vertical-align: middle; font-weight: bold; }   
  296.   
  297. .page_num {background-color: #FFFFFF; border: 1px solid #DEDEB8; margin-right:1px; vertical-align: middle; }   
  298.   
  299. a:hover.page_num  {background-color: #F5FBFF; border: 1px solid #86B9D6; text-decoration: none; }   
  300.   
  301. .page_curpage { margin-right:1px; border: 1px solid #DEDEB8; vertical-align: middle; background-color: #FFFF80; color: #92A05A; font-weight: bold; }   
  302.   
  303. .page_bar { margin: 1px 0px; clear: both; }   
  304.   
  305. .page_bar a { float: left; padding: 1px 4px; font-size: 12px; text-decoration: none; }   
  306.   
  307. .page_redirect { background-color: #FFFFFF; border: 1px solid #DEDEB8; margin-right:1px; font-size: 12px !important; font-size: 13px; }   
  308.   
  309. a:hover.page_redirect { background-color: #F5FBFF; border: 1px solid #86B9D6; text-decoration: none; }   
  310.   
  311. </style>");  
  312.  
  313.             #endregion   
  314.   
  315.   
  316.   
  317.             writeStr.Append("<table cellspacing='0' cellpadding='0' align='left' style='clear: both;'>");   
  318.   
  319.             writeStr.Append("<tr><td valign='bottom'>");   
  320.   
  321.             writeStr.Append("<div class='page_bar'>");   
  322.   
  323.   
  324.   
  325.             writeStr.Append("<a class='page_total' title='记录总数'> " + _recTotal.ToString() + " </a>");   
  326.   
  327.             writeStr.Append("<a class='page_pages' title='当前页/总页数'> " + PageIndex + "/" + PageTotal + " </a>");   
  328.   
  329.   
  330.   
  331.             string url = GetUrl().ToLower();//取得当前页面的完整URL   
  332.   
  333.             string tmp;   
  334.   
  335.   
  336.   
  337.             //没有page时,加一个page, 方便后面替换   
  338.   
  339.             if (url.IndexOf("page=") < 0)   
  340.   
  341.             {   
  342.   
  343.                 url = url + ((url.IndexOf("?") < 0) ? "?" : "&") + "page=1";   
  344.   
  345.             }   
  346.   
  347.   
  348.   
  349.             //设定正则表达式,用于替换url中的page=字符串   
  350.   
  351.             Regex my = new Regex(@"(page=[0-9]*(end)*)", RegexOptions.IgnoreCase);  
  352.  
  353.  
  354.  
  355.             #region 如果当前是指定显示个数的页码之后的页时,加上到第一页和上一页的链接(此时页面不会显示页码1)   
  356.   
  357.             if (PageIndex > _pageShowNum + 1)   
  358.   
  359.             {   
  360.   
  361.                 //第一页   
  362.   
  363.                 tmp = my.Replace(url, "page=1");   
  364.   
  365.                 writeStr.Append("<a class='page_redirect' href='" + tmp + "' title='第一页'>|<</a>");   
  366.   
  367.                 //上一页   
  368.   
  369.                 tmp = my.Replace(url, "page=" + (PageIndex - 1).ToString());   
  370.   
  371.                 writeStr.Append("<a class='page_redirect' href='" + tmp + "' title='上一页'><</a>");   
  372.   
  373.             }  
  374.  
  375.             #endregion   
  376.   
  377.   
  378.   
  379.             //总共显示( _pageShowNum * 2 + 1 )页的链接,从当前页往前_pageShowNum页,往前不足_pageShowNum页时,后面的页数增加.   
  380.   
  381.             int countPage = 0;//用于统计是否超过( _pageShowNum * 2 + 1 )页   
  382.   
  383.             int loopStart;   
  384.   
  385.   
  386.   
  387.             //当总页数大于( _pageShowNum * 2 + 1 ),且当前页为最后_pageShowNum页时,保持页面页码数不少于( _pageShowNum * 2 + 1 )   
  388.   
  389.             if (PageTotal - PageIndex >= _pageShowNum)   
  390.   
  391.             {   
  392.   
  393.                 // 总页数 减 当前页码 大于等于左边要显示的个数时,循环 从当前页码 大于等于左边要显示的个数 开始   
  394.   
  395.                 loopStart = PageIndex - _pageShowNum;   
  396.   
  397.             }   
  398.   
  399.             else  
  400.   
  401.             {   
  402.   
  403.                 loopStart = PageTotal - _pageShowNum * 2;// PageIndex - _pageShowNum - (_pageShowNum - PageTotal + PageIndex) ==> PageTotal - _pageShowNum * 2   
  404.   
  405.             }   
  406.   
  407.   
  408.   
  409.             for (int loop = loopStart; loop <= PageTotal; loop++)   
  410.   
  411.             {   
  412.   
  413.                 if (loop < 1)   
  414.   
  415.                     continue;   
  416.   
  417.                 else  
  418.   
  419.                     countPage++;  
  420.  
  421.  
  422.  
  423.                 #region 已经显示了( _pageShowNum * 2 + 1 )页 并且 当前页数小于等于总页数时,加上到最末页和上一页的链接并退出循环(不会显示最后1页的页码时)   
  424.   
  425.                 if (countPage > (_pageShowNum * 2 + 1) && loop <= PageTotal)   
  426.   
  427.                 {   
  428.   
  429.                     //下一页   
  430.   
  431.                     tmp = my.Replace(url, "page=" + (PageIndex + 1).ToString());   
  432.   
  433.                     writeStr.Append("<a class='page_redirect' href='" + tmp + "' title='下一页'>></a>");   
  434.   
  435.                     //最终页   
  436.   
  437.                     tmp = my.Replace(url, "page=" + PageTotal.ToString());// my.Replace(url, "page=end");  
  438.   
  439.                     writeStr.Append("<a class='page_redirect' href='" + tmp + "' title='最末页'>>|</a>");   
  440.   
  441.                     break;   
  442.   
  443.                 }  
  444.  
  445.                 #endregion  
  446.  
  447.  
  448.  
  449.                 #region 添加当前循环页的链接   
  450.   
  451.                 if (loop == PageIndex)   
  452.   
  453.                 {//当前页   
  454.   
  455.                     writeStr.Append("<a class='page_curpage' style='background-color:" + _color + "'>" +    
  456.   
  457.                         loop.ToString() + "</a>");// style='width:" + (loop > 99 ? (width + 5).ToString() : width.ToString()) + "px;'  
  458.   
  459.                 }   
  460.   
  461.                 else  
  462.   
  463.                 {   
  464.   
  465.                     //计算下一页的链接   
  466.   
  467.                     tmp = my.Replace(url, "page=" + loop.ToString());   
  468.   
  469.                     writeStr.Append("<a class='page_num' href='" + tmp + "' title='第" +    
  470.   
  471.                         loop.ToString() + "页''>" + loop.ToString() + "</a>");   
  472.   
  473.                 }  
  474.  
  475.                 #endregion   
  476.   
  477.             }   
  478.   
  479.             writeStr.Append("</div></td></tr></table>");   
  480.   
  481.   
  482.   
  483.             writer.Write(writeStr.ToString());   
  484.   
  485.         }   
  486.   
  487.   
  488.   
  489.         /// <summary>   
  490.   
  491.         /// 获取当前页面的完整URL地址信息   
  492.   
  493.         /// </summary>   
  494.   
  495.         /// <returns></returns>   
  496.   
  497.         public string GetUrl()   
  498.   
  499.         {   
  500.   
  501.             string url;   
  502.   
  503.   
  504.   
  505.             url = System.Web.HttpContext.Current.Request.ServerVariables["SERVER_NAME"];   
  506.   
  507.   
  508.   
  509.             if (System.Web.HttpContext.Current.Request.ServerVariables["SERVER_PORT"] != "80")   
  510.   
  511.                 url += ":" + System.Web.HttpContext.Current.Request.ServerVariables["SERVER_PORT"];   
  512.   
  513.             //strTemp = strTemp & CheckStr(Request.ServerVariables("URL"))    
  514.   
  515.   
  516.   
  517.             url += System.Web.HttpContext.Current.Request.ServerVariables["SCRIPT_NAME"];   
  518.   
  519.             if (!(System.Web.HttpContext.Current.Request.QueryString == null || System.Web.HttpContext.Current.Request.QueryString.ToString() == ""))   
  520.   
  521.             {   
  522.   
  523.                 url += "?" + System.Web.HttpContext.Current.Request.QueryString.ToString();   
  524.   
  525.             }   
  526.   
  527.             string https = System.Web.HttpContext.Current.Request.ServerVariables["HTTPS"];   
  528.   
  529.             if (https == null || https == string.Empty || https == "off")   
  530.   
  531.             {   
  532.   
  533.                 url = "http://" + url;   
  534.   
  535.             }   
  536.   
  537.             else  
  538.   
  539.             {   
  540.   
  541.                 url = "https://" + url;   
  542.   
  543.             }   
  544.   
  545.             return url;   
  546.   
  547.         }   
  548.   
  549.     }   
  550.   
  551. }  

具体的控件代码如下,在你的VS.Net里新建一个用户控件,然后把下面的代码拷贝进去就可以用了,最下面是使用示

 

posted @ 2011-10-18 16:29  lanmiao  阅读(210)  评论(0编辑  收藏  举报