一个简单实用的分页控件

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace localhost
{
 /// <summary>
 /// Pager 的摘要说明。
 /// </summary>
 public class Pager : Control
 {
  private string _UrlFormat;
  private int _PageSize;
  private int _RecordCount;
  private int _PageCount = 5;

  /// <summary>
  /// 连接格式
  /// </summary>
  public string UrlFormat
  {
   get
   {
    return _UrlFormat;
   }
   set
   {
    _UrlFormat = value;
   }
  }

  /// <summary>
  /// 页长度
  /// </summary>
  public int PageSize
  {
   get
   {
    return _PageSize;
   }
   set
   {
    _PageSize = value;
   }
  }

  /// <summary>
  /// 当前页码
  /// </summary>
  public int PageIndex
  {
   get
   {
    string Pageindex = HttpContext.Current.Request.QueryString["PageIndex"];
    if ( Pageindex != null )
    {
     return int.Parse(Pageindex);
    }
    return 1;
   }
  }

  /// <summary>
  /// 总记录数
  /// </summary>
  public int RecordCount
  {
   get
   {
    return _RecordCount;
   }
   set
   {
    _RecordCount = value;
   }
  }

  /// <summary>
  /// 两边显示个数
  /// </summary>
  public int PageCount
  {
   get
   {
    return _PageCount;
   }
   set
   {
    _PageCount = value;
   }
  }

  protected override void Render(HtmlTextWriter writer)
  {
   int SumPage = (RecordCount + PageSize - 1)/PageSize;

   int start = PageIndex - PageCount;
   int end = PageIndex + PageCount;

   //以PageIndex为中心,前后个显示Page个页码导航
   if (SumPage>(PageCount*2+1))
   {
    if (start<1)
    {
     start = 1;
     end = start + 10;
    }
    else if (end>SumPage)
    {
     start = SumPage - 10;
     end = SumPage;
    }
   }
   else
   {
    start = 1;
    end = SumPage;
   }
   


   string tmp = "<a href=/"" + UrlFormat + "/">[{0}]</a>";
   StringBuilder sb = new StringBuilder();
   if (PageIndex > 1)
   {
    sb.Append(string.Format("<a href=/"" + UrlFormat + "/">首页</a>", 1));
    sb.Append(string.Format("<a href=/"" + UrlFormat + "/">上一页</a>", PageIndex - 1));
   }
   for (int i = start; i <= end; i++)
   {
    if (i==PageIndex)
    {
     sb.Append("[" + PageIndex.ToString() + "]");
    }
    else
    {
     sb.Append(string.Format(tmp, i));
    }
    sb.Append("&nbsp;");
   }
   if (PageIndex < SumPage)
   {
    sb.Append(string.Format("<a href=/"" + UrlFormat + "/">下一页</a>", PageIndex + 1));
    sb.Append(string.Format("<a href=/"" + UrlFormat + "/">尾页</a>", SumPage));
   }
   writer.Write(sb.ToString());
  }

 }
}
调用示例:

前台代码

<%@ Register NameSpace="localhost" TagPrefix="hh" assembly="localhost" %>
<hh:Pager id="Pager1" PageSize="20" UrlFormat="/{0}.aspx" runat="server"></hh:Pager>

后台代码

protected Pager Pager1;
Pager1.RecordCount = 32455;


 

你可能感兴趣的:(String,assembly,null,System,Class)