分页控件 WebPager [ZT]

两个类文件,点这里下载DLL文件>WebPager.dll
1、 WebPager.cs

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

[assembly:TagPrefix("WebPager", "wp")]
namespace Xiaoqiao.Controls.WebPager
{
 public delegate void WebPagerEventHandler(object sender, EventArgs e);
 /// <summary>
 /// WebPager 的摘要说明。
 /// </summary>
 [ DefaultProperty("CurrentPageIndex") ]
 [ ToolboxData("<{0}:WebPagerControl runat=server></{0}:WebPagerControl>") ]
 [ Designer("Xiaoqiao.Controls.WebPager.WebPagerDesigner") ]
 [ ParseChildren(false) ]
 public class WebPagerControl : WebControl,INamingContainer,IPostBackEventHandler
 {
  int m_CurrentPageIndex = 0;
  int m_PageCount = 0;
  int m_PageSize = 1;
  int m_RecordCount = 0;
  int m_NavNums = 10;
  System.Drawing.Color m_CurrentPageNumColor = System.Drawing.Color.Red;
  System.Collections.Specialized.NameValueCollection _aa;

  string m_FirstPageText = "|&lt;";
  string m_PrevPageText = "&lt;&lt;";
  string m_NextPageText = "&gt;&gt;";
  string m_LastPageText = "&gt;|";

  //定义事件代理
  public event WebPagerEventHandler PageChange;
  public WebPagerControl()
  {
  }
  protected override void Render(System.Web.UI.HtmlTextWriter writer)
  {
   if ( m_PageCount==0 )
   {
    base.Render(writer);
    return;
   }
   int n = m_NavNums;
   int r = m_CurrentPageIndex%n;
   int startIndex = (m_CurrentPageIndex-r)/n * n;
   int endIndex = startIndex + n - 1;

   if (endIndex>=m_PageCount)
    endIndex = m_PageCount-1;

   if ( m_PageCount==1||m_CurrentPageIndex==0 )
   {
    writer.Write(m_FirstPageText);
    writer.Write("&nbsp;");
    writer.Write(m_PrevPageText);
    writer.Write("&nbsp;");
   }
   else
   {
    writer.AddAttribute(HtmlTextWriterAttribute.Href,"javascript:"+Page.GetPostBackEventReference(this,"0"));
    writer.RenderBeginTag(HtmlTextWriterTag.A);
    writer.Write(m_FirstPageText);
    writer.RenderEndTag();
    writer.Write("&nbsp;");
    writer.AddAttribute(HtmlTextWriterAttribute.Href,"javascript:"+Page.GetPostBackEventReference(this,(m_CurrentPageIndex-1).ToString()));
    writer.RenderBeginTag(HtmlTextWriterTag.A);
    writer.Write(m_PrevPageText);
    writer.RenderEndTag();
    writer.Write("&nbsp;");
   }

   for ( int i=startIndex;i<=endIndex;i++ )
   {
    string s = i.ToString();
    if ( m_CurrentPageIndex==i )
    {
     writer.Write("["+(i+1).ToString()+"]");
     writer.Write("&nbsp;");
    }
    else
    {
     writer.AddAttribute(HtmlTextWriterAttribute.Href,"javascript:"+Page.GetPostBackEventReference(this,s));
     writer.RenderBeginTag(HtmlTextWriterTag.A);
     writer.Write("["+(i+1).ToString()+"]");
     writer.RenderEndTag();
     writer.Write("&nbsp;");
    }
   }

   if ( m_PageCount==1||m_CurrentPageIndex==(m_PageCount-1) )
   {
    writer.Write(m_NextPageText);
    writer.Write("&nbsp;");
    writer.Write(m_LastPageText);
   }
   else
   {
    writer.AddAttribute(HtmlTextWriterAttribute.Href,"javascript:"+Page.GetPostBackEventReference(this,(m_CurrentPageIndex+1).ToString()));
    writer.RenderBeginTag(HtmlTextWriterTag.A);
    writer.Write(m_NextPageText);
    writer.RenderEndTag();
    writer.Write("&nbsp;");
    writer.AddAttribute(HtmlTextWriterAttribute.Href,"javascript:"+Page.GetPostBackEventReference(this,(m_PageCount-1).ToString()));
    writer.RenderBeginTag(HtmlTextWriterTag.A);
    writer.Write(m_LastPageText);
    writer.RenderEndTag();
   }

   string inputId = this.ClientID+"_pagenum";
   writer.AddAttribute(HtmlTextWriterAttribute.Type,"text");
   writer.AddAttribute(HtmlTextWriterAttribute.Size,"3");
   writer.AddAttribute(HtmlTextWriterAttribute.Id,inputId);
   writer.AddAttribute(HtmlTextWriterAttribute.Value,(m_CurrentPageIndex+1).ToString());
   writer.RenderBeginTag(HtmlTextWriterTag.Input);
   writer.RenderEndTag();
   writer.AddAttribute(HtmlTextWriterAttribute.Onclick,"javascript:"+this.ClientID+"_GoToPage();");
   writer.AddAttribute(HtmlTextWriterAttribute.Type,"button");
   writer.AddAttribute(HtmlTextWriterAttribute.Value,"go");
   writer.RenderBeginTag(HtmlTextWriterTag.Input);
   writer.RenderEndTag();

   base.Render(writer);
  }

  private void OnPageChange(object sender, EventArgs e)
  {
   this.ViewState["CurrentPageIndex"] = m_CurrentPageIndex;
   if (PageChange != null)
    PageChange(sender,e);
  }

  protected override void OnLoad(EventArgs e)
  {
   object state;

   state = this.ViewState["CurrentPageIndex"];
   if (state != null)
    m_CurrentPageIndex = Convert.ToInt32(state);

   state = this.ViewState["RecordCount"];
   if (state != null)
    this.RecordCount = Convert.ToInt32(state);

   string inputId = this.ClientID+"_pagenum";
   string scriptKey = "webpager_script";
   if ( !Page.IsClientScriptBlockRegistered(scriptKey) )
   {
    string script = "<script language=javascript>function "+this.ClientID+"_GoToPage(){";
    script += "var gotoPage=parseInt(document.getElementById('"+inputId+"').value);";
    script += "if (isNaN(gotoPage)){alert('请输入正确的页码');return;}";
    script += "if ( gotoPage>"+m_PageCount+"||gotoPage<1 ){alert('页码超出范围');return;}gotoPage--;";
    script += "__doPostBack('"+this.ClientID+"',gotoPage)"+";";//Page.GetPostBackEventReference(this,"gotoPage")+";";
    script += "}</script>";
    Page.RegisterClientScriptBlock(scriptKey,script);
   }

   base.OnLoad(e);
  }


  public int CurrentPageIndex
  {
   get { return m_CurrentPageIndex; }
   set { m_CurrentPageIndex = value; }
  }
  public int RecordCount
  {
   get
   {
    return m_RecordCount;
   }
   set
   {
    int i = value;
    this.ViewState["RecordCount"] = i;
    m_PageCount = GetPages(i,m_PageSize);
    m_RecordCount = i;
   }
  }
  public int PageSize
  {
   get
   {
    return m_PageSize;
   }
   set
   {
    int i = value;
    if (i>0)
    {
     m_PageCount = GetPages(m_RecordCount,i);
     m_PageSize = i;
    }
    else
     throw(new Exception("PageSize必须大于零"));
   }
  }
  private int GetPages(int Total,int PageSize)
  {
   int i = Total;
   int t;
   if ( (i%PageSize)==0 )
    t = i/PageSize;
   else
    t = (int)(i-i%PageSize)/PageSize+1;
   return t;
  }

  public int NavNums
  {
   get
   {
    return m_NavNums;
   }
   set
   {
    int i = value;
    if (i>0)
     m_NavNums = value;
   }
  }
  public string FirstPageText
  {
   get
   {
    return m_FirstPageText;
   }
   set
   {
    m_FirstPageText = value;
   }
  }
  public string PrevPageText
  {
   get
   {
    return m_PrevPageText;
   }
   set
   {
    m_PrevPageText = value;
   }
  }
  public string NextPageText
  {
   get
   {
    return m_NextPageText;
   }
   set
   {
    m_NextPageText = value;
   }
  }
  public string LastPageText
  {
   get
   {
    return m_LastPageText;
   }
   set
   {
    m_LastPageText = value;
   }
  }
  public System.Drawing.Color CurrentPageNumColor
  {
   get
   {
    return m_CurrentPageNumColor;
   }
   set
   {
    m_CurrentPageNumColor = value;
   }
  }

  public System.Collections.Specialized.NameValueCollection aa
  {
   get { return _aa; }
   set { _aa = value; }
  }

  #region IPostBackEventHandler 成员

  public void RaisePostBackEvent(string eventArgument)
  {
   // TODO:  添加 WebPagerControl.RaisePostBackEvent 实现
   CurrentPageIndex = int.Parse(eventArgument);
   OnPageChange(this,null);
  }

  #endregion
 }
}

2、WebPagerDesigner.cs

using System;
using System.Globalization;

namespace Xiaoqiao.Controls.WebPager
{
 /// <summary>
 /// WebPagerDesigner 的摘要说明。
 /// </summary>
 public class WebPagerDesigner : System.Web.UI.Design.ControlDesigner
 {
  public WebPagerDesigner()
  {
  }
  public override string GetDesignTimeHtml()
  {
   WebPagerControl oControl = (WebPagerControl)Component ;
   return String.Format( CultureInfo.InvariantCulture,
    "<table width=\"{0}\" height=\"{1}\" bgcolor=\"#f5f5f5\" bordercolor=\"#c7c7c7\" cellpadding=\"0\" cellspacing=\"0\" border=\"1\"><tr><td valign=\"middle\" align=\"center\">WebPagerControl - <b>{2}</b></td></tr></table>",
    oControl.Width,
    oControl.Height,
    oControl.ID ) ;
  }
 }
}

posted @ 2007-10-25 12:54  RobotTech  阅读(724)  评论(0编辑  收藏  举报