• 分页控件入门


    转自:http://www.cnblogs.com/ASPNET2008/archive/2008/03/24/1119668.html

    文章写的很有入门特征,学写控件,收藏了。

    在解决方案中,添加一个web控件库,命名为pagerControls ,然后把默认创建的类更名为:WebCustomControlPager 
    将文章的所有代码复制过去,就可能编译成一个控件dll 。

    CODE:

      1 using System;
      2 using System.Collections.Generic;
      3 using System.ComponentModel;
      4 using System.Text;
      5 using System.Web;
      6 using System.Web.UI;
      7 using System.Web.UI.WebControls;
      8 
      9 namespace pagerControls
     10 {
     11     [DefaultProperty("Text")]
     12     [ToolboxData("<{0}:WebCustomControlPager runat=server></{0}:WebCustomControlPager>")]
     13     public class WebCustomControlPager : WebControl
     14     {
     15         private string _pageUrl = "";
     16         /// <summary>
     17         /// 当前页面地址
     18         /// </summary>
     19         [Bindable(true)]
     20         [CategoryAttribute("Appearance")]
     21         [DefaultValue("")]
     22         [Localizable(true)]
     23         [DescriptionAttribute("当前页面地址")]
     24         public virtual string pageUrl
     25         {
     26             get
     27             {
     28                 return this._pageUrl;
     29             }
     30 
     31             set
     32             {
     33                 this._pageUrl = value;
     34             }
     35         }
     36         private int _currentPageIndex;
     37         /// <summary>
     38         /// 当前页面索引
     39         /// </summary>
     40         [Bindable(true)]
     41         [CategoryAttribute("Appearance")]
     42         [Localizable(true)]
     43         [DescriptionAttribute("当前页面索引从1开始")]
     44         [DefaultValueAttribute("当前页面索引")]
     45         public virtual int currentPageIndex
     46         {
     47             get
     48             {
     49                 return this._currentPageIndex;
     50             }
     51 
     52             set
     53             {
     54                 this._currentPageIndex = value;
     55             }
     56         }
     57         private int _IsCustomStyle;
     58         /// <summary>
     59         /// 是否自定义样式
     60         /// 0:不是 1:是
     61         /// </summary>
     62         [Bindable(true)]
     63         [CategoryAttribute("Appearance")]
     64         [Localizable(true)]
     65         [DescriptionAttribute("是否自定义样式")]
     66         [DefaultValueAttribute("0")]
     67         public virtual int IsCustomStyle
     68         {
     69             get
     70             {
     71                 return this._IsCustomStyle;
     72             }
     73 
     74             set
     75             {
     76                 this._IsCustomStyle = value;
     77             }
     78         }
     79         private int _iRecordCount = 1;
     80         /// <summary>
     81         /// 记录数量
     82         /// </summary>
     83         [Bindable(true)]
     84         [CategoryAttribute("Appearance")]
     85         [Localizable(true)]
     86         [DescriptionAttribute("记录数量")]
     87         [DefaultValueAttribute("记录数量")]
     88         public virtual int iRecordCount
     89         {
     90             get
     91             {
     92                 return this._iRecordCount;
     93             }
     94 
     95             set
     96             {
     97                 this._iRecordCount = value;
     98             }
     99         }
    100         private int _iRowsCount = 10;
    101         /// <summary>
    102         /// 每页记录数量
    103         /// </summary>
    104         [Bindable(true)]
    105         [CategoryAttribute("Appearance")]
    106         [Localizable(true)]
    107         [DescriptionAttribute("每页记录数量")]
    108         [DefaultValueAttribute("每页记录数量")]
    109         public virtual int iRowsCount
    110         {
    111             get
    112             {
    113                 return this._iRowsCount;
    114             }
    115 
    116             set
    117             {
    118                 this._iRowsCount = value;
    119             }
    120         }
    121         private int _iPrevCount = 5;
    122         /// <summary>
    123         /// 前部分记录数量
    124         /// </summary>
    125         [Bindable(true)]
    126         [CategoryAttribute("Appearance")]
    127         [Localizable(true)]
    128         [DescriptionAttribute("前部分记录数量")]
    129         [DefaultValueAttribute("前部分记录数量")]
    130         public virtual int iPrevCount
    131         {
    132             get
    133             {
    134                 return this._iPrevCount;
    135             }
    136 
    137             set
    138             {
    139                 this._iPrevCount = value;
    140             }
    141         }
    142         private int _iNextCount = 5;
    143         /// <summary>
    144         /// 后部分记录数量
    145         /// </summary>
    146         [Bindable(true)]
    147         [CategoryAttribute("Appearance")]
    148         [Localizable(true)]
    149         [DescriptionAttribute("后部分记录数量")]
    150         [DefaultValueAttribute("后部分记录数量")]
    151         public virtual int iNextCount
    152         {
    153             get
    154             {
    155 
    156                 return this._iNextCount;
    157             }
    158 
    159             set
    160             {
    161                 this._iNextCount = value;
    162             }
    163         }
    164         /// <summary>
    165         /// 取得默认样式表信息
    166         /// by minjiang 08-3-24
    167         /// </summary>
    168         /// <returns></returns>
    169         private string styleHtml()
    170         {
    171             //分页样式表信息
    172             string sStyle = "";
    173             StringBuilder strbStyle = new StringBuilder();
    174             sStyle = "<style type =\"text/css\" >";
    175             strbStyle.Append(sStyle);
    176             //样式内容
    177             sStyle = "</style>";
    178             strbStyle.Append(sStyle);
    179             //样式内容结尾
    180             sStyle = strbStyle.ToString();
    181             return sStyle;
    182         }
    183         protected override void RenderContents(HtmlTextWriter output)
    184         {
    185             //分页样式表信息
    186             string sStyle = "";
    187             if (this.IsCustomStyle == 0)
    188             {
    189                 sStyle = this.styleHtml();
    190 
    191             }
    192             //分页信息
    193             string sPagerHtml = "";
    194             sPagerHtml = this.CSgetPagerHtml(this.iRecordCount, this.iRowsCount, this.pageUrl, this.currentPageIndex);
    195             sPagerHtml = sStyle + sPagerHtml;
    196             output.Write(sPagerHtml);
    197         }
    198 
    199         /// <summary>
    200         /// 取得分页信息
    201         /// by minjiang 08-3-11
    202         /// </summary>
    203         /// <param name="recordCount">记录总数</param>
    204         /// <param name="iRowsCount">每页记录大小</param>
    205         /// <param name="pageUrl">页面地址</param>
    206         /// <returns></returns>
    207         public string CSgetPagerHtml(int recordCount, int iRowsCount, string pageUrl, int iCurrentPageIndex)
    208         {
    209             int intPrevPageCount = this.iPrevCount;
    210             int intNextPageCount = this.iNextCount;
    211             //如果小于此数字则不显示省略号
    212             int intDefaultCount = this.iNextCount + this.iPrevCount;
    213             StringBuilder strb = new StringBuilder();
    214             string info = "";
    215             info = "<div class=\"survey_pagediv\"><div class=\"pagedivcenter\"><div class=\"page\">";
    216             strb.Append(info);
    217             //链接地址
    218             if (pageUrl.IndexOf("page="!= -1)
    219             {
    220                 int pageIndex = pageUrl.IndexOf("page=");
    221                 pageUrl = pageUrl.Substring(0, pageIndex + 5);
    222             }
    223             else
    224             {
    225                 //如果页面没有任何参数则加上?与参数
    226                 if (pageUrl.IndexOf("?"== -1)
    227                 {
    228                     pageUrl += "?page=";
    229                 }
    230                 else
    231                 {
    232                     //如果只是没有page参数则加上些参数
    233                     pageUrl += "&page=";
    234                 }
    235 
    236             }
    237             //页面总数
    238             int pageCount = 1;
    239             if (recordCount > 0)
    240             {
    241                 if (recordCount % iRowsCount == 0)
    242                 { pageCount = recordCount / iRowsCount; }
    243                 else
    244                 {
    245                     if (recordCount > iRowsCount)
    246                     {
    247                         //如果页面数量大于1
    248                         pageCount = recordCount / iRowsCount + 1;
    249                     }
    250                     else
    251                     {
    252                         //不显示分页信息
    253                         return "";
    254 
    255                     }
    256                 }
    257             }
    258             if (pageCount < 1)
    259             {
    260                 //如果只有一页则不显示分页信息
    261                 return "";
    262 
    263             }
    264             int currentPage = iCurrentPageIndex;
    265             //int.TryParse(sCurrentPageIndex, out currentPage);
    266             if (currentPage < 1)
    267             {
    268                 currentPage = 1;
    269             }
    270             //上一页索引
    271             int prevPage = 1;
    272             prevPage = currentPage - 1;
    273             if (prevPage < 1)
    274             { prevPage = 1; }
    275             //下一页索引
    276             int nextPage = 1;
    277             nextPage = currentPage + 1;
    278             if (nextPage > pageCount)
    279             { nextPage = 1; }
    280             //开始的索引页
    281             int startPageIndex = currentPage;
    282             //要统计的页面总数
    283             int pageTotalCount = pageCount - currentPage + 1;
    284             if (currentPage != 1)
    285             {
    286                 //如果当前页面不是首页则显示上一页
    287                 info = "<span><a href=\"" + pageUrl + prevPage.ToString() + "\">&lt;&lt;上一页 </a></span>";
    288                 strb.Append(info);
    289             }
    290 
    291             //回首页
    292             info = "<span><a href=\"" + pageUrl + "1\">首页</a></span>";
    293             strb.Append(info);
    294             string pageClass = "num a4";
    295             //当前页的样式为select a5
    296             //如果页数小于等于10页则不显示省略号
    297             if (pageCount <= intDefaultCount)
    298             {
    299                 for (int kk = 1; kk <= pageCount; kk++)
    300                 {
    301                     pageClass = "num a4";
    302                     if (kk == currentPage)
    303                     {
    304                         pageClass = "select a5";
    305 
    306 
    307                     }
    308                     info = "<a href=\"" + pageUrl + kk.ToString() + "\" class=\"" + pageClass + "\">" + kk.ToString() + "</a>";
    309                     strb.Append(info);
    310 
    311                 }
    312                 //下一页地址
    313                 //info = "<span><a href=\"" + pageUrl + nextPage.ToString() + "\">下一页&gt;&gt;</a></span>";
    314                 //strb.Append(info);
    315                 info = strb.ToString();
    316                 return info;
    317 
    318             }
    319             //省略号前显示5页
    320             if (pageTotalCount <= intDefaultCount)
    321             {
    322                 //如果要统计的页面数量小于分页前部分数量加分页后部分数量
    323                 //要统计的页面数量与分页前部分数量加分页后部分数量的差额
    324                 //分页栏总是显示分页前部分数量加分页后部分数量个页面链接
    325                 int iDispersion = intDefaultCount - pageTotalCount;
    326                 for (int k = currentPage - iDispersion; k <= pageCount; k++)
    327                 {
    328                     pageClass = "num a4";
    329                     if (k == currentPage)
    330                     {
    331                         pageClass = "select a5";
    332 
    333                     }
    334                     info = "<a href=\"" + pageUrl + k.ToString() + "\" class=\"" + pageClass + "\">" + k.ToString() + "</a>";
    335                     strb.Append(info);
    336                 }
    337             }
    338             else
    339             {
    340 
    341                 for (int k = currentPage; k <= currentPage + intPrevPageCount - 1; k++)
    342                 {
    343                     pageClass = "num a4";
    344                     if (k == currentPage)
    345                     {
    346                         pageClass = "select a5";
    347                     }
    348                     info = "<a href=\"" + pageUrl + k.ToString() + "\" class=\"" + pageClass + "\">" + k.ToString() + "</a>";
    349                     strb.Append(info);
    350 
    351                 }
    352                 if (pageTotalCount > (intPrevPageCount + intNextPageCount))
    353                 {
    354                     //如果要统计的页面总数大于省略号前的记录加省略号后面的记录数量则显示 ""
    355                     info = " <span></span>";
    356                     strb.Append(info);
    357                 }
    358 
    359                 //开始索引
    360                 int _iNextSatrIndex = pageCount - intNextPageCount + 1;
    361                 for (int j = _iNextSatrIndex; j <= pageCount; j++)
    362                 {
    363                     pageClass = "num a4";
    364                     if (j == currentPage)
    365                     {
    366                         pageClass = "select a5";
    367 
    368                     }
    369                     info = "<a href=\"" + pageUrl + j.ToString() + "\" class=\"" + pageClass + "\">" + j.ToString() + "</a>";
    370                     strb.Append(info);
    371 
    372                 }
    373             }
    374             //回末页
    375             if (currentPage != pageCount)
    376             {
    377                 //如果不是最后一页则显示下一页
    378                 info = "<span><a href=\"" + pageUrl + nextPage.ToString() + "\">下一页&gt;&gt;</a></span>";
    379                 strb.Append(info);
    380             }
    381 
    382             info = "</div></div></div>";
    383             strb.Append(info);
    384             info = strb.ToString();
    385             return info;
    386 
    387         }
    388     }
    389 }
    390 
    391 

    前台页面:

    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

    <%@ Register Assembly="pagerControls" Namespace="pagerControls" TagPrefix="cc1" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        
    <title>無題のページ</title>
    </head>
    <body>
        
    <form id="form1" runat="server">
        
    <div>
            
    <cc1:WebCustomControlPager ID="WebCustomControlPager1" iNextCount ="5" iPrevCount ="5"  runat="server" />
        
    </div>
        
    </form>
    </body>
    </html>

    后台代码:

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    public partial class _Default : System.Web.UI.Page 
    {
        
    protected void Page_Load(object sender, EventArgs e)
        {
            
    this.WebCustomControlPager1.pageUrl = this.Request.Url.ToString();
            
    int iPage = 1;
            iPage 
    = 1;
            
            
    if (iPage < 1)
            { iPage 
    = 1; }
            
    this.WebCustomControlPager1.currentPageIndex = iPage;
            
    this.WebCustomControlPager1.iNextCount = 5;
            
    this.WebCustomControlPager1.iPrevCount = 5;
            
    this.WebCustomControlPager1.iRowsCount = 10;
            
    this.WebCustomControlPager1.iRecordCount = 120;
            
    this.WebCustomControlPager1.IsCustomStyle = 1;
        }
    }




  • 相关阅读:
    Jenkins构建项目
    jenkins的理解及安装
    GitLab初识以及代码迁移
    深入浅出Git(偏向理论)
    理解下所谓的ssh隧道
    洛谷-P1908 逆序对
    洛谷-P1010 幂次方
    洛谷-P1226 【模板】快速幂||取余运算
    洛谷-P1074 靶形数独
    洛谷-P1433 吃奶酪
  • 原文地址:https://www.cnblogs.com/tonybinlj/p/1296634.html
Copyright © 2020-2023  润新知