shawl.qiu c# .net DataList, Repeater, DataGrid 分页类 v1.0

shawl.qiu c# .net DataList, Repeater, DataGrid 分页类 v1.0

说明:
其实 DataGrid 本身就自带分页...不过鄙人很讨厌 Table, 虽然也可以设置为Flow, 但灵活性还是打折了, 应用虽然非常方便, 但是不灵活.
从自由点来看, 鄙人比较喜欢灵活性较高的 DataList, 可发觉没自带分页功能, 这大概就是什么鱼与熊掌, 不可兼得的原因吧...
因此这个类就诞生了...

Pagination 类 使用重载, 使得使用最少的代码可让分页功能应用于:
DataList, Repeater, DataGrid.

目录:
1. x.aspx
2. cs/Pagination.cs

下载原格式:
http://files.myopera.com/btbtd/csharp/class/sq_csharp_pagination_class_v1.0.7z

shawl.qiu
2007-02-09
http://blog.csdn.net/btbtd

内容:
1. x.aspx
  1. <%@ Page Language="C#AutoEventWireup="True" %>
  2. <%@ Assembly src="cs/Pagination.cs" %>
  3. <%@ import Namespace="System.Data" %>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5. <html xmlns="http://www.w3.org/1999/xhtml">
  6. <head>
  7. <meta http-equiv="Content-Typecontent="text/html; charset=utf-8" />
  8. <title>shawl.qiu template</title>
  9. <style type="text/css">
  10. /*<![CDATA[*/
  11. .sqPagedCurLink{
  12.     color: #FFCCFF;
  13.  font-weight:bold;
  14. }

  15. .sqPagedNonLink{
  16.  color:#ccc;
  17. }

  18. .sqPagedJumpBox{
  19.  border:0px;
  20.  border-bottom:1px dashed black;
  21.  width:48px;
  22. }
  23. /*]]>*/
  24. </style>
  25. <script runat="server">
  26.  void Page_Load(Object s, EventArgs e)
  27.  { 
  28.   DataTable dt =new DataTable();
  29.   DataRow dr;
  30.   
  31.   dt.Columns.Add(new DataColumn("id", typeof(Int32)));
  32.   dt.Columns.Add(new DataColumn("item", typeof(String)));
  33.   
  34.   for(Int32 i=0; i<222; i++)
  35.   {
  36.    dr=dt.NewRow();
  37.    dr[0]=i;
  38.    dr[1]="item "+i.ToString();
  39.    dt.Rows.Add(dr);
  40.   }
  41.   
  42.   Pagination Paged=new Pagination();
  43.    Paged.Debug=false;                      // 是否为调试模式
  44.    Paged.DebugLabel=glrDebugLabel;         // 显示调试信息的 Label
  45.    Paged.NavigatorLabel=glrPagedLabel;     // 显示主导航条的 Label
  46.    Paged.DetailsLabel=glrPagedDetailsLabel;// 显示分页明细的 Label
  47.    Paged.PageSize=10;                      // 每页大小 
  48.    Paged.Go(dt, glrFileList);              // 执行分页
  49.    Paged=null;
  50.   
  51.  } // end Page_Load
  52. </script>
  53. </head>
  54. <body>
  55.  <form runat="server">
  56.   <div class="algc">
  57.    <asp:Label id="glrDebugLabelrunat="server" />
  58.   </div>
  59.   <div class="algc">
  60.    <asp:Label id="glrPagedLabelrunat="server" />
  61.    <div class="algc">
  62.     <asp:Label id="glrPagedDetailsLabelrunat="server" />
  63.    </div>
  64.   </div>
  65.   <asp:DataList id="glrFileList"
  66.    BorderColor="black"
  67.    CellPadding="5"
  68.    CellSpacing="5"
  69.    RepeatDirection="Horizontal"
  70.    RepeatLayout="Flow"
  71.    RepeatColumns="5"
  72.    ShowBorder="True"
  73.    runat="server">
  74.  
  75.    <HeaderTemplate>
  76.     <div>
  77.    </HeaderTemplate>
  78.    
  79.    <HeaderStyle BackColor="#aaaadd">
  80.    </HeaderStyle>
  81.   
  82.    <AlternatingItemStyle BackColor="Gainsboro">
  83.    </AlternatingItemStyle>
  84.     
  85.    <ItemTemplate
  86.     <href="?id=<%# DataBinder.Eval(Container.DataItem, "id") %>">
  87.      <%# DataBinder.Eval(Container.DataItem, "item") %>
  88.     </a>&nbsp;
  89.    </ItemTemplate>   
  90. <%--
  91.    <SeparatorTemplate
  92.    </SeparatorTemplate>
  93. --%>
  94.    <FooterTemplate>
  95.     </div>
  96.    </FooterTemplate>
  97.   </asp:DataList>
  98.  </form>
  99. </body>
  100. </html>
  101.  
2. cs/Pagination.cs
  1. using System;
  2. using System.Data;
  3. using System.Web.UI.WebControls;
  4. using System.Text.RegularExpressions;

  5. /*-----------------------------------------------------------------------------------*/
  6.  * shawl.qiu c# .net Pagination class v1.0
  7. /*-----------------------------------------------------------------------------------*/
  8. //---------------------------------------------------------------------begin class Pagination
  9. public class Pagination
  10. {
  11.  //-----------------------------------begin event
  12.  public Pagination()
  13.  {
  14.  }
  15.  
  16.  ~Pagination()
  17.  {
  18.  }
  19.  //-----------------------------------end event
  20.  
  21.  //-----------------------------------begin public constant
  22.  //-----------------------begin about
  23.  public const String auSubject="shawl.qiu c# .net Pagination class";
  24.  public const String auVersion="v1.0";
  25.  public const String au="shawl.qiu";
  26.  public const String auEmail="shawl.qiu@gmail.com";
  27.  public const String auBlog="http://blog.csdn.net/btbtd";
  28.  public const String auCreateDate="2007-2-9";
  29.  //-----------------------end about
  30.  //-----------------------------------end public constant
  31.  
  32.  //-----------------------------------begin public variable
  33.  public Int32 PageSize=10;
  34.  public Int32 CurrentPage=0;
  35.  public Int32 ListNumSize=10;
  36.  
  37.  public String QueryId="page";
  38.  
  39.  public Boolean Debug=false;
  40.  
  41.  public Label DebugLabel;
  42.  public Label NavigatorLabel;
  43.  public Label DetailsLabel;
  44.  
  45.  public String WordFirst="首页";
  46.  public String WordPreviousTen="上十页";
  47.  public String WordPrevious="上一页";
  48.  public String WordNext="下一页";
  49.  public String WordNextTen="下十页";
  50.  public String WordLast="末页";
  51.  //-----------------------------------end public variable
  52.  
  53.  //-----------------------------------begin private variable
  54.  private Int32 PageCount=0;
  55.  private Int32 DataSourceCount=0;
  56.  
  57.  private String QueryUrl=""; 
  58.  //-----------------------------------end private variable
  59.  
  60.  //-----------------------------------begin public method
  61.  public void Go(DataTable dtDataTable, DataList dsDataList)
  62.  {
  63.   if(dtDataTable==null||dsDataList==null)goto End;
  64.    dsDataList.DataSource=objPagedDataSource(dtDataTable); 
  65.    dsDataList.DataBind();
  66.   End:;
  67.  }
  68.  public void Go(DataTable dtDataTable, Repeater rptRepeater)
  69.  {
  70.   if(dtDataTable==null||rptRepeater==null)goto End;
  71.    rptRepeater.DataSource=objPagedDataSource(dtDataTable); 
  72.    rptRepeater.DataBind();
  73.   End:;
  74.  }
  75.  public void Go(DataTable dtDataTable, DataGrid dgDataGrid)
  76.  {
  77.   if(dtDataTable==null||dgDataGrid==null)goto End;
  78.    dgDataGrid.DataSource=objPagedDataSource(dtDataTable); 
  79.    dgDataGrid.DataBind();
  80.   End:;
  81.  }
  82.  //-----------------------------------end public method
  83.  
  84.  //-----------------------------------begin private method
  85.  private PagedDataSource objPagedDataSource(DataTable dtDataTable)
  86.  { 
  87.   PagedDataSource dtPaged=new PagedDataSource();
  88.   dtPaged.DataSource = dtDataTable.DefaultView; 
  89.   dtPaged.AllowPaging = true
  90.   dtPaged.PageSize = PageSize; 
  91.   DataSourceCount=dtPaged.DataSourceCount;
  92.   
  93.   Int32 PageCount=dtPaged.PageCount;
  94.   
  95.   if(System.Web.HttpContext.Current.Request.QueryString[QueryId]!=null)
  96.   {
  97.    String sTemp=System.Web.HttpContext.Current.Request.QueryString[QueryId];
  98.    CurrentPage=Int32.Parse(sTemp);
  99.   }
  100.   
  101.   if(CurrentPage+1>PageCount)
  102.   {
  103.    CurrentPage=PageCount;
  104.   }
  105.   if(CurrentPage<1)
  106.   {
  107.    CurrentPage=1;
  108.   }
  109.   
  110.   AutoConvertPagedUrl(QueryId, out QueryUrl);
  111.   
  112.   if(Debug&&DebugLabel!=null)
  113.   {
  114.    DebugLabel.Text+="<li/>总记录数: "+DataSourceCount;
  115.    DebugLabel.Text+="<li/>总页数: "+PageCount;
  116.    DebugLabel.Text+="<li/>当前页: "+CurrentPage;
  117.    DebugLabel.Text+="<li/>每页大小: "+PageSize;
  118.    DebugLabel.Text+="<li/>QueryUrl "+QueryUrl;
  119.    
  120.    DebugLabel.Text+="<hr/>";
  121.   }
  122.   
  123.   if(NavigatorLabel!=null)
  124.   {
  125.    if(CurrentPage!=1)
  126.    {
  127.     NavigatorLabel.Text="<a href='"+QueryUrl+"1'>"+WordFirst+"</a> ";
  128.    }
  129.    else 
  130.    {
  131.     NavigatorLabel.Text+="<span class='sqPagedNonLink'>"+WordFirst+"</span> ";
  132.    }
  133.    
  134.    if(Debug)
  135.    {
  136.     DebugLabel.Text+="<li/>"+WordPreviousTen+": "+(CurrentPage-(CurrentPage%10)-10);
  137.     DebugLabel.Text+="<hr/>";
  138.    }
  139.    
  140.    if(CurrentPage-(CurrentPage%10)-10>=0)
  141.    {
  142.     NavigatorLabel.Text+="<a href='"+QueryUrl+
  143.      (CurrentPage-(CurrentPage%10)-10+1)+"'>"+WordPreviousTen+"</a> ";
  144.    }
  145.    else 
  146.    {
  147.     NavigatorLabel.Text+="<span class='sqPagedNonLink'>"+WordPreviousTen+"</span> ";
  148.    }
  149.    
  150.    if(CurrentPage>1)
  151.    {
  152.     NavigatorLabel.Text+="<a href='"+QueryUrl+(CurrentPage-1)+"'>"+WordPrevious+"</a> ";
  153.    }
  154.    else 
  155.    {
  156.     NavigatorLabel.Text+="<span class='sqPagedNonLink'>"+WordPrevious+"</span> ";
  157.    }
  158.    
  159.    Int32 LoopNum=CurrentPage-(CurrentPage%10)+1;
  160.    if(Debug)
  161.    {
  162.     DebugLabel.Text+="<li/>数字链接循环起始: "+LoopNum;
  163.     DebugLabel.Text+="<hr/>";
  164.    }
  165.    
  166.    for(Int32 i=1; i<=ListNumSize; i++)
  167.    {
  168.     if(LoopNum>PageCount)break;
  169.     if(LoopNum==CurrentPage)
  170.     {
  171.      NavigatorLabel.Text+="<span class='sqPagedCurLink'>"+(LoopNum)+"</span> ";
  172.     }
  173.     else 
  174.     {
  175.      NavigatorLabel.Text+="<a href='"+QueryUrl+(LoopNum)+"'>"+(LoopNum)+"</a> ";
  176.     }
  177.     LoopNum++;
  178.    }
  179.    
  180.    if(CurrentPage<PageCount)
  181.    {
  182.     NavigatorLabel.Text+="<a href='"+QueryUrl+(CurrentPage+1)+"'>"+WordNext+"</a> ";
  183.    }
  184.    else 
  185.    {
  186.     NavigatorLabel.Text+="<span class='sqPagedNonLink'>"+WordNext+"</span> ";
  187.    }
  188.    
  189.    if(Debug)
  190.    {
  191.     DebugLabel.Text+="<li/>下十页: "+(CurrentPage-(CurrentPage%10)+10);
  192.     DebugLabel.Text+="<hr/>";
  193.    }
  194.    
  195.    if(CurrentPage-(CurrentPage%10)+10<=PageCount)
  196.    {
  197.     NavigatorLabel.Text+="<a href='"+QueryUrl+
  198.      (CurrentPage-(CurrentPage%10)+11)+"'>"+WordNextTen+"</a> ";
  199.    }
  200.    else 
  201.    {
  202.     NavigatorLabel.Text+="<span class='sqPagedNonLink'>"+WordNextTen+"</span> ";
  203.    }
  204.    
  205.    if(CurrentPage!=PageCount)
  206.    {
  207.     NavigatorLabel.Text+="<a href='"+QueryUrl+(PageCount)+"'>"+WordLast+"</a> ";
  208.    }
  209.    else 
  210.    {
  211.     NavigatorLabel.Text+="<span class='sqPagedNonLink'>"+WordLast+"</span> ";
  212.    }
  213.    PagedJumpBox(NavigatorLabel, QueryUrl);
  214.   }
  215.  
  216.   if(DetailsLabel!=null)
  217.   {
  218.    DetailsLabel.Text=
  219.     PageSize+"篇/页 "+
  220.     (CurrentPage)+"/"+(PageCount)+
  221.     "页 共"+DataSourceCount+"篇";
  222.   }
  223.   
  224.   dtPaged.CurrentPageIndex =(CurrentPage-1);
  225.   return dtPaged;
  226.  } // end private PagedDataSource objPagedDataSource
  227.  
  228.  private void PagedJumpBox(Label NavigatorLabel, String QueryUrl)
  229.  {
  230.   NavigatorLabel.Text+="转到:/n";
  231.   NavigatorLabel.Text+=
  232.    "<input type='text' onsubmit='return false;' size='6' "+
  233.    "class='sqPagedJumpBox' onkeypress='fG2Url(this.value, this.form,event)' />/n";

  234.   NavigatorLabel.Text+="<script type='text/javascript'>/n";
  235.   NavigatorLabel.Text+="//<![CDATA[/n";
  236.   NavigatorLabel.Text+=function fG2Url(sUrl, oForm, oEvt){/n";
  237.   NavigatorLabel.Text+="  oForm.onsubmit=function()/n";
  238.   NavigatorLabel.Text+="  {/n";
  239.   NavigatorLabel.Text+="   return false;/n";
  240.   NavigatorLabel.Text+="  }/n";
  241.   NavigatorLabel.Text+="  if(!oEvt)var oEvt=window.event;/n";
  242.   NavigatorLabel.Text+="  var kc=oEvt.which||oEvt.keyCode;/n";
  243.   NavigatorLabel.Text+="  if(kc==13){/n";
  244.   NavigatorLabel.Text+=@"  sUrl=sUrl.replace(/^/s+|/s+$/g, '');";
  245.   NavigatorLabel.Text+=@"  sUrl=parseInt(sUrl)||1;";
  246.   NavigatorLabel.Text+=@"  window.location.href='"+QueryUrl+"'+sUrl}";
  247.   NavigatorLabel.Text+=" }/n";
  248.   NavigatorLabel.Text+="//]]>/n";
  249.   NavigatorLabel.Text+="</script>/n";
  250.  } // end private void PagedJumpBox
  251.  
  252.  private void AutoConvertPagedUrl(String QueryId, out String QueryUrl)
  253.  {
  254.   QueryUrl=System.Web.HttpContext.Current.Request.Url+"";
  255.   if(QueryUrl.IndexOf(@"?")==-1)
  256.   {
  257.    QueryUrl+=@"?";
  258.   }
  259.   QueryUrl=Regex.Replace(QueryUrl,@"/b"+@QueryId+@"/=[^&]+","", RegexOptions.IgnoreCase);
  260.   QueryUrl+="&"+QueryId+"=";
  261.   QueryUrl=Regex.Replace(QueryUrl,@"/?/&",@"?", RegexOptions.IgnoreCase);
  262.   QueryUrl=Regex.Replace(QueryUrl,@"/&+",@"&", RegexOptions.IgnoreCase);
  263.  } // end private void AutoConvertPagedUrl
  264.  //-----------------------------------end private method
  265. }
  266. //---------------------------------------------------------------------end class Pagination 

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值