网站导航网学 原创论文 原创专题 网站设计 最新系统 原创论文 论文降重 发表论文 论文发表 UI设计定制 论文答辩PPT格式排版 期刊发表 论文专题
返回网学首页
网学原创论文
最新论文 推荐专题 热门论文 论文专题
当前位置: 网学 > 设计资源 > .Net编程 > 正文

仿博客园的分页控件

论文降重修改服务、格式排版等 获取论文 论文降重及排版 论文发表 相关服务

在demo 中 有css 文件。在使用页面中引入即可 下载  /uploadfile/201007/23/956588609.rar

看一下分页效果。

使用方法

  1. using System;  
  2. using System.Collections;  
  3. using System.Configuration;  
  4. using System.Data;  
  5. using System.Linq;  
  6. using System.Web;  
  7. using System.Web.Security;  
  8. using System.Web.UI;  
  9. using System.Web.UI.HtmlControls;  
  10. using System.Web.UI.WebControls;  
  11. using System.Web.UI.WebControls.WebParts;  
  12. using System.Xml.Linq;  
  13. using Tension.Web.Controls;  
  14. using Tension.Extension;  
  15.    
  16. namespace PagingBarDemo  
  17. {  
  18.     public partial class _Default : System.Web.UI.Page  
  19.     {  
  20.         protected void Page_Load(object sender, EventArgs e)  
  21.         {  
  22.             string pageIndex = Request.QueryString["pageIndex"] ?? "1";  
  23.             int index = 1;  
  24.             pageIndex.IsInt32(out index);  
  25.    
  26.             PagingBar defaultBar = new PagingBar();  
  27.             defaultBar.LeftSize = 4;  
  28.             defaultBar.PageCount = 40;  
  29.             defaultBar.PageIndex = index;  
  30.             defaultBar.Url = "Default.aspx?pageIndex={0}";  
  31.      defaultBar.Target = "_black";  
  32.             Literal1.Text = defaultBar.BuildLinkHtml();  
  33.    
  34.             PagingBar bar2 = new PagingBar("scott");  
  35.             bar2.LeftSize = 4;  
  36.             bar2.PageCount = 40;  
  37.             bar2.PageIndex = index;  
  38.             bar2.Url = "Default.aspx?pageIndex={0}";  
  39.             Literal2.Text = bar2.BuildLinkHtml();  
  40.  
  41.  
  42.             PagingBar bar3 = new PagingBar("green-black");  
  43.             bar3.LeftSize = 4;  
  44.             bar3.PageCount = 40;  
  45.             bar3.PageIndex = index;  
  46.             bar3.Url = "Default.aspx?pageIndex={0}";  
  47.             Literal3.Text = bar3.BuildLinkHtml();  
  48.    
  49.             PagingBar bar4 = new PagingBar("quotes");  
  50.             bar4.LeftSize = 4;  
  51.             bar4.PageCount = 40;  
  52.             bar4.PageIndex = index;  
  53.             bar4.Url = "Default.aspx?pageIndex={0}";  
  54.             Literal4.Text = bar4.BuildLinkHtml();  
  55.                
  56.         }  
  57.     }  
  58. }  
  1. PagingBar defaultBar = new PagingBar();  
  2.             defaultBar.LeftSize = 4;   //左边显示的链接数 也就是说 当这个参数为 4 时 实际显示 9个链接 总算显示的是奇数个链接  
  3.             defaultBar.PageCount = 40;     //总的页数  
  4.             defaultBar.PageIndex = index;  //当前页   
  5.             defaultBar.Url = "Default.aspx?pageIndex={0}"//链接的URL   
  6.  
  7.      defaultBar.Target = "_black"//页面在浏览器窗口中的打开方式 可以为空  
  8.             Literal1.Text = defaultBar.BuildLinkHtml(); //产生HTML 输出  

这里是默认构造 默认构造的话回去读取 web.config 需找配置的 CSS 样式名

需要在 appSettings 节点下添加 配置

  1. <appSettings> 
  2.     <add key="PagingBarStyle" value="yellow"/> 
  3.  
  4. </appSettings> 

不是默认构造的话 可以使用 一个带参构造

  1. PagingBar bar4 = new PagingBar("quotes"); 

直接指定 css 名称

完整类代码
 

  1. using System;    
  2. using System.Data;    
  3. using System.Configuration;    
  4. using System.Web;    
  5. using System.Web.Security;    
  6. using System.Web.UI;    
  7. using System.Web.UI.WebControls;    
  8. using System.Web.UI.WebControls.WebParts;    
  9. using System.Web.UI.HtmlControls;    
  10. using System.Text;    
  11.      
  12.  
  13. /// <summary>    
  14. /// 分页控件类        
  15. /// </summary>    
  16. ///     
  17. namespace Tension.Web.Controls    
  18. {    
  19.     public class PagingBar : System.Web.UI.UserControl    
  20.     {    
  21.         string style = "";    
  22.         public PagingBar()    
  23.         {    
  24.             //样式    
  25.             style = ConfigurationManager.AppSettings["PagingBarStyle"];    
  26.             if (string.IsNullOrEmpty(style))    
  27.             {    
  28.                 throw new ArgumentNullException("无法在 Web.config 文件的 AppSettings 节点下找到有关 PagingBarStyle 的配置信息。");    
  29.             }    
  30.         }        
  31.         public PagingBar(string style)    
  32.         {    
  33.             //样式    
  34.             this.style = style;    
  35.         }    
  36.         //当前页    
  37.         private int pageIndex;        
  38.         /// <summary>    
  39.         /// 当前页    
  40.         /// </summary>    
  41.         public int PageIndex    
  42.         {    
  43.             get { return pageIndex; }    
  44.             set { pageIndex = value; }    
  45.         }    
  46.      
  47.         //总页数    
  48.         private int pageCount;        
  49.         /// <summary>    
  50.         /// 总页数    
  51.         /// </summary>    
  52.         public int PageCount    
  53.         {    
  54.             get { return pageCount; }    
  55.             set { pageCount = value; }    
  56.         }        
  57.         //左边显示连接数    
  58.         private int leftSize;        
  59.         /// <summary>    
  60.         /// 左边显示连接数    
  61.         /// </summary>    
  62.         public int LeftSize    
  63.         {    
  64.             get { return leftSize; }    
  65.             set { leftSize = value; }    
  66.  
  67.         }        
  68.         //连接地址    
  69.         private string url;        
  70.  
  71.         /// <summary>    
  72.         /// 连接地址    
  73.         /// </summary>    
  74.         public string Url    
  75.         {    
  76.             get { return url; }    
  77.             set { url = value; }    
  78.         }       
  79.         //目标    
  80.         private string target;    
  81.      
  82.         /// <summary>    
  83.         /// 目标    
  84.         /// </summary>    
  85.         public string Target    
  86.         {    
  87.             get { return target; }    
  88.             set { target = value; }    
  89.         }    
  90.      
  91.         public string BuildLinkHtml()    
  92.         {            
  93.             //目标    
  94.             string target = Target;        
  95.             //处理目前字符串    
  96.             if (!string.IsNullOrEmpty(target))    
  97.             {    
  98.                 target = "target=\"" + target + "\"";    
  99.             }    
  100.      
  101.             //需要输出的HTML    
  102.             StringBuilder html = new StringBuilder("<div id=\"pagingbar\" class=\"pagingbar\"><div class=\"");    
  103.             html.Append(style);    
  104.             html.Append("\">");  
  105.      
  106.             #region 输出上一页    
  107.             if (PageIndex <= 1)    
  108.             {    
  109.                 html.Append("<span class=\"disabled\">< Prev </span>");    
  110.             }    
  111.             else   
  112.             {    
  113.                 html.Append("<a href=\"");    
  114.                 html.Append(string.Format(Url, PageIndex - 1));    
  115.                 html.Append("\" ");    
  116.                 html.Append(target);    
  117.                 html.Append(">< Prev </a>");    
  118.             }  
  119.             #endregion        
  120.             //总长度    
  121.             int barSize = LeftSize * 2 + 1;  
  122.      
  123.             #region 输出中间    
  124.             //页数小于0    
  125.             if (PageCount <= 0)    
  126.             {    
  127.                 return "<center><span>页数为0!!</span></center>";    
  128.             }    
  129.      
  130.             //总页数小于正常显示的条数 则全部显示出来    
  131.             if (PageCount <= barSize)    
  132.             {    
  133.                 for (int i = 1; i <= PageCount; i++)    
  134.                 {    
  135.                     if (PageIndex == i)    
  136.                     {    
  137.                         html.Append("<span class=\"current\">");    
  138.                         html.Append(i);    
  139.                         html.Append("</span>");    
  140.                     }    
  141.                     else   
  142.                     {    
  143.                         html.Append("<a href=\"");    
  144.                         html.Append(string.Format(Url, i));    
  145.                         html.Append("\" ");    
  146.                         html.Append(target);    
  147.                         html.Append(">");    
  148.                         html.Append(i);    
  149.                         html.Append("</a>");    
  150.                     }    
  151.                 }    
  152.             }    
  153.             else   
  154.             {    
  155.                 if (PageIndex < leftSize + 1)    
  156.                 {    
  157.                     for (int i = 1; i <= barSize; i++)    
  158.                     {    
  159.                         if (PageIndex == i)    
  160.                         {    
  161.                             html.Append("<span class=\"current\">");    
  162.                             html.Append(i);    
  163.                             html.Append("</span>");    
  164.                         }    
  165.                         else   
  166.                         {    
  167.                             html.Append("<a href=\"");    
  168.                             html.Append(string.Format(Url, i));    
  169.                             html.Append("\" ");    
  170.                             html.Append(target);    
  171.                             html.Append(">");    
  172.                             html.Append(i);    
  173.                             html.Append("</a>");    
  174.                         }    
  175.                     }    
  176.      
  177.                     html.Append("...<a href=\"");    
  178.                     html.Append(string.Format(Url, PageCount));    
  179.                     html.Append("\" ");    
  180.                     html.Append(target);    
  181.                     html.Append(">");    
  182.                     html.Append(PageCount);    
  183.                     html.Append("</a>");    
  184.                 }    
  185.                 else   
  186.                 {    
  187.                     html.Append("<a href=\"");    
  188.                     html.Append(string.Format(Url, 1));    
  189.                     html.Append("\" ");    
  190.                     html.Append(target);    
  191.                     html.Append(">");    
  192.                     html.Append(1);    
  193.                     html.Append("</a>...");        
  194.      
  195.                     if (PageIndex < PageCount - LeftSize)    
  196.                     {    
  197.                         for (int i = PageIndex - leftSize; i < PageIndex; i++)    
  198.                         {    
  199.                             if (i == 1)    
  200.                             {    
  201.                                 continue;    
  202.                             }    
  203.      
  204.                             html.Append("<a href=\"");    
  205.                             html.Append(string.Format(Url, i));    
  206.                             html.Append("\" ");    
  207.                             html.Append(target);    
  208.                             html.Append(">");    
  209.                             html.Append(i);    
  210.                             html.Append("</a>");    
  211.                         }    
  212.                         for (int i = PageIndex; i <= PageIndex + leftSize; i++)    
  213.                         {    
  214.                             if (PageIndex == i)    
  215.                             {    
  216.                                 html.Append("<span class=\"current\">");    
  217.                                 html.Append(i);    
  218.                                 html.Append("</span>");    
  219.                             }    
  220.                             else   
  221.                             {    
  222.                                 html.Append("<a href=\"");    
  223.                                 html.Append(string.Format(Url, i));    
  224.                                 html.Append("\" ");    
  225.                                 html.Append(target);    
  226.                                 html.Append(">");    
  227.                                 html.Append(i);    
  228.  
  229.                                 html.Append("</a>");    
  230.                             }    
  231.                         }    
  232.                         html.Append("...<a href=\"");    
  233.                         html.Append(string.Format(Url, PageCount));    
  234.                         html.Append("\" ");    
  235.                         html.Append(target);    
  236.                         html.Append(">");    
  237.                         html.Append(PageCount);    
  238.                         html.Append("</a>");        
  239.                     }    
  240.                     else   
  241.                     {    
  242.                         for (int i = PageCount - LeftSize - LeftSize; i <= PageCount; i++)    
  243.                         {    
  244.                             if (PageIndex == i)    
  245.                             {    
  246.                                 html.Append("<span class=\"current\">");    
  247.                                 html.Append(i);    
  248.                                 html.Append("</span>");    
  249.                             }    
  250.                             else   
  251.                             {    
  252.                                 html.Append("<a href=\"");    
  253.                                 html.Append(string.Format(Url, i));    
  254.                                 html.Append("\" ");    
  255.                                 html.Append(target);    
  256.                                 html.Append(">");    
  257.                                 html.Append(i);    
  258.                                 html.Append("</a>");    
  259.                             }    
  260.                         }    
  261.                     }       
  262.                 }        
  263.             }  
  264.             #endregion      
  265.  
  266.             #region 输出下一页    
  267.             if (PageIndex >= PageCount)    
  268.             {    
  269.                 html.Append("<span class=\"disabled\"> Next > </span>");    
  270.             }    
  271.             else   
  272.             {    
  273.                 html.Append("<a href=\"");    
  274.                 html.Append(string.Format(Url, PageIndex + 1));    
  275.                 html.Append("\" ");    
  276.                 html.Append(target);    
  277.                 html.Append("> Next > </a>");    
  278.             }  
  279.             #endregion        
  280.             html.Append("</div></div>");    
  281.             return html.ToString();    
  282.         }    
  283.     }    
  284. }   
设为首页 | 加入收藏 | 网学首页 | 原创论文 | 计算机原创
版权所有 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
Copyright 2008-2020 myeducs.Cn www.myeducs.Cn All Rights Reserved 湘ICP备09003080号 常年法律顾问:王律师