SimplePager

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace KF.Web.Controls
{
 /// <summary>
 /// SimplePager 的摘要说明。
 /// </summary>
 public class SimplePager : Repeater {
 //public class SimplePager : DataGrid {

  //Static constants
  protected const string HTML1 = "<table width=96% border=0 cellpadding=0 cellspacing=0 align=center><tr><td colspan=2>";
  protected const string HTML2 = "</td></tr><tr class=gridNav><td>总数:{0} 总页数:{1} 当前页数:{2}";
  protected const string HTML3 = "</td><td align=right>";
  protected const string HTML4 = "</td></tr></table>";
  protected const string LINK_TEXTBOX = "<asp:textbox id=txtRemark Columns=20 MaxLength=11 Runat=server></asp:textbox>";
  private static readonly Regex RX = new Regex(@"^&page=/d+", RegexOptions.Compiled);
  //private const string LINK_PREV = "<a href=?page={0}><img src=Images/buttonPrev.gif alt=Previous border=/"0/"></a>";
  //private const string LINK_MORE = "<a href=?page={0}><img src=Images/buttonMore.gif alt=More border=/"0/"></a>";
  private const string LINK_PREV = "<a href=?page={0}>上一页</a>";
  //private LinkButton LINK_PREV = new LinkButton();
  private const string LINK_MORE = "<a href=?page={0}>下一页</a>";
  private const string LINK_FIRST = "<a href=?page={0}>第一页</a>";
  private const string LINK_LAST = "<a href=?page={0}>最后一页</a>";
  private const string LINK_TURN = "<a href=?page={0}>跳转</a>";
  
  private const string KEY_PAGE = "page";
  private const string COMMA = "?";
  private const string AMP = "&";

  protected string _emptyText;
  private IList _dataSource;
  private int _pageSize = 10;
  private int _currentPageIndex;
  private int _itemCount;

  /*public SimplePager()
  {
   LINK_PREV.Text = "pre";
   LINK_PREV.ID = "pre";
   this.LINK_PREV.Click += new System.EventHandler(this.btnAdd_Click);
  }

  private void btnAdd_Click(object sender, System.EventArgs e)
  {
   string a = "1";
   a = "b";  
  }*/

  
  override public object DataSource {
   set {
    //This try catch block is to avoid issues with the VS.NET designer
    //The designer will try and bind a datasource which does not derive from ILIST
    try{
     _dataSource = (IList)value;
     ItemCount = _dataSource.Count;
//HttpContext.Current.Response.Write("<br>|"+_dataSource.Count);
    }catch{
     _dataSource = null;
     ItemCount = 0;
    }
   }
  }

  public int PageSize {
   get { return _pageSize; }
   set { _pageSize = value; }
  }

  protected int PageCount {
   get { return (ItemCount - 1) / _pageSize; }
  }

  virtual protected int ItemCount {
   get { return _itemCount; }
   set { _itemCount = value; }
  }

  virtual public int CurrentPageIndex {
   get {
    return _currentPageIndex; }
   set { _currentPageIndex = value; }
  }

  public string EmptyText {
   set { _emptyText = value; }
  }

  public void SetPage(int index)
  {
   OnPageIndexChanged(new DataGridPageChangedEventArgs(null, index));
  }


  override protected void OnLoad(EventArgs e) {

   if (Visible) {
    string page = Context.Request[KEY_PAGE];
    int index = (page != null) ? int.Parse(page) : 0;
    SetPage(index);
   }
  }


  /// <summary>
  /// Overriden method to control how the page is rendered
  /// </summary>
  /// <param name="writer"></param>
  override protected void Render(HtmlTextWriter writer)
  {

   //Check there is some data attached

   if (ItemCount == 0) {
    writer.Write(_emptyText);
    return;
   }

   //Mask the query
   string query = Context.Request.Url.Query.Replace(COMMA, AMP);
   query = RX.Replace(query, string.Empty);

   // Write out the first part of the control, the table header
   writer.Write(HTML1);
   
   // Call the inherited method
   base.Render(writer);

   // Write out a table row closure
   //writer.Write(HTML2);
   writer.Write(string.Format(HTML2, _itemCount,PageCount+1,_currentPageIndex+1));

   //Close the table data tag
   writer.Write(HTML3);

   writer.Write(LINK_TEXTBOX);
           

   //writer.Write(string.Format(LINK_TURN, 35 + query));
   
   //Determin whether next and previous buttons are required
   //Previous button?
   if (_currentPageIndex > 0)
   {
    
    writer.Write(string.Format(LINK_FIRST, 0 + query));
    writer.Write(string.Format(LINK_PREV, (_currentPageIndex - 1) + query));
   }

   //Next button
   if (_currentPageIndex < PageCount)
   {
    writer.Write(string.Format(LINK_MORE, (_currentPageIndex + 1) + query));
    writer.Write(string.Format(LINK_LAST, PageCount + query));
   }

   //Close the table
   writer.Write(HTML4);
  }

  override protected void OnDataBinding(EventArgs e)
  {

   //Work out which items we want to render to the page
   int start = CurrentPageIndex * _pageSize;
   int size = Math.Min(_pageSize, ItemCount - start);
   IList page = new ArrayList();

   //Add the relevant items from the datasource
   for (int i = 0; i < size; i++)
    page.Add(_dataSource[start + i]);
   
   //set the base objects datasource
   base.DataSource = page;   
   base.OnDataBinding(e);
  }

  // 申明 PageIndexChanged 事件的类型是 DataGridPageChangeEventHandler
  // 这是一个委托类型.要处理 PageIndexChanged 事件,您必须提供一个签名为EventHandler的事件处理程序(事件处理方法)
  public event DataGridPageChangedEventHandler PageIndexChanged;

  virtual protected void OnPageIndexChanged(DataGridPageChangedEventArgs e)
  {

   if (PageIndexChanged != null) {
    PageIndexChanged(this, e);
   }
  }
 }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值