discoverx

博客园 首页 新随笔 联系 订阅 管理

public class page : System.Web.UI.WebControls.WebControl
 {
  public page()
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //
   // 已经解决中文参数问题。

  }
  private int _count;//每页显示的记录条数

  private int _CurrentPage;//当前页

  private int _allCount;//所有记录条数

  private int _showPages;//显示页数

 

  //以下脚本用于从文本框输入页码

  private const string SCRIPTSTRING = " \n";

  [DefaultValue(10),Category("Customer")]

  public int Count

  {
   set
   {
    if(value <= 0)

     _count = 1;

    else

     _count = value;

   }
   get
   {

    return _count;

   }

  }

  [DefaultValue(1),Category("Customer")]

  public int CurrentPage

  {

   set

   {

    if(value < 0)

     _CurrentPage = 1;

    else

     _CurrentPage = value;

   }

   get

   {

    return _CurrentPage;

   }

  }

 

  [DefaultValue(1),Category("Customer")]

  public int AllCount

  {

   get

   {

    return _allCount;

   }

   set

   {

    if(_allCount < 0)

     throw new Exception("记录总条数不能为负数");

    else

     _allCount = value;

   }

  }

 

  [DefaultValue(1),Category("Customer")]

  public int Pages//总页数

  {

   get

   {

    if(this._allCount % this._count > 0)

     return ((int)this._allCount / this._count) + 1;

    else

     return ((int)this._allCount / this._count);

   }

  }

 

  [DefaultValue(1),Category("Customer")]

  public int ShowPages

  {

   set

   {

    if(value > 0)

     _showPages = value;

    else

     _showPages = 9;

   }

   get

   {

    return _showPages;

   }

  }

 

  protected override void Render(HtmlTextWriter writer)

  {

   base.Render (writer);

 

   string leftInfo;

   StringBuilder centerInfo = new StringBuilder();

 

   //分页条分三部分,leftInfo是最左边的部分,用来显示当前页/总页数,每页显示的记录条数

   leftInfo = "页" + this.CurrentPage.ToString() + "/" +

    this.Pages.ToString() + "  " + "每页" + this.Count.ToString() + "条" + "  共" +

    this.AllCount.ToString() + "条";    

           

   //中间的部分是分页部分

   int min;//要显示的页面数最小值

   int max;//要显示的页面数最大值

 

   if(this.CurrentPage > this.Pages)//当前页必须小于最大页
   {
    this.CurrentPage = this.Pages;
   }

 

   if(this.CurrentPage % this.ShowPages == 0) //如果恰好整除
   {

    if(this.AllCount == 0)
    {
     min = this.CurrentPage;

     max = this.CurrentPage+this.ShowPages;
    }
    else if(this.AllCount <= (this.CurrentPage*this.Count))
    {
     min = this.CurrentPage-this.ShowPages + 1;

     max = this.CurrentPage;
    }
    else
    {
     min = this.CurrentPage-2;

     max = this.CurrentPage + this.ShowPages-2;
    }

   }

   else if(this.CurrentPage % this.ShowPages == 1 && this.CurrentPage >

    this.ShowPages )

   {

    if(this.AllCount <= (this.CurrentPage*this.Count))
    {
     min = (((int)this.CurrentPage / this.ShowPages ) - 1) *

      this.ShowPages +2;

     max = this.CurrentPage;
    }
    else
    {
     min = this.CurrentPage;

     max = this.CurrentPage+this.ShowPages;
    }

   }

   else

   {

    min = ((int)this.CurrentPage / this.ShowPages) * this.ShowPages +

     1;

    max = (((int)this.CurrentPage / this.ShowPages) +1) *

     this.ShowPages;

   }          

 

   string numberStr = " ";//循环生成数字序列部分

   string AbsUrl = String.Empty;//URL?左边的部分

   //处理中间含有中文出错的问题   怎么样处理格空?

   
   string temps;
   int tempi;
   if(this.Context.Request.Url.ToString().IndexOf("?")>0)
   {
    temps=this.Context.Request.Url.ToString().Replace(" ","20%");
    temps=temps.Substring(0,temps.IndexOf(""));
    for(tempi=0;tempi<this.Context.Request.QueryString.Count;tempi++)
    {
     if(tempi==0)
      temps=temps+"?"+this.Context.Request.QueryString.Keys[tempi].ToString()+"="+this.Context.Request.QueryString.Get(tempi).ToString();
     else
      temps=temps+"&"+this.Context.Request.QueryString.Keys[tempi].ToString()+"="+this.Context.Request.QueryString.Get(tempi).ToString();
    }
    
    AbsUrl=temps;
   }
   else
    AbsUrl = (this.Context.Request.Url.ToString());

   //处理参数出现中文错误结束


   if(AbsUrl.IndexOf("?") == -1)

   {              
    AbsUrl+="?";
   }

   else

   {
    string s=AbsUrl.Substring(AbsUrl.IndexOf("?"));
    s=s.ToLower();
    if(s.IndexOf("currentpage")==-1)      //不存在currentpage变量
    {
     AbsUrl+="&";
    }
    else         //多个参数时,currentpage必须放在最右边才能正确
    {
     AbsUrl=AbsUrl.Substring(0,AbsUrl.IndexOf("?"))+s.Substring(0,s.IndexOf("currentpage"));
    }
    
   }

 

   for(int i = min ; i <= max ; i++)

   {          

    if(i <= this.Pages)//只有不大于最大页才显示

    {

     if(this.CurrentPage == i)//如果是当前页,用斜体和红色显示

     {

      numberStr = numberStr + "<I style='color:red'>" + i.ToString() + "</I>" +"</a>" + "\n";


     }

     else

     {
      numberStr = numberStr + "<a href=" + AbsUrl + "CurrentPage=" + i.ToString() + ">" + i.ToString() +"</a>" + "\n";

     }

    }

   }

 

   //第一页,上一页,下一页,最后一页

   string First,Previous,Next,Last;
  
   if(CurrentPage<=1)

    First = "";

   else

    First = "<a href="+AbsUrl + "CurrentPage=1"+">";
   /////////

   if(CurrentPage <= 1)

    Previous="";

   else

    Previous = "<a href="+AbsUrl + "CurrentPage=" + (CurrentPage- 1).ToString()+">";

   /////////

   if(CurrentPage == this.Pages)
    
    Next="";

   else

    Next = "<a href="+AbsUrl + "CurrentPage=" + (CurrentPage + 1).ToString()+">";

   /////////
  
   if(CurrentPage==this.Pages)

    Last="";
   else

    Last = "<a href="+AbsUrl + "CurrentPage=" + this.Pages+">";

   /////////
   centerInfo.AppendFormat("<font face='Webdings' style='font-size:14px'>{0}9</a>{1}7</a></font>{2}<font face='Webdings' style='font-size:14px'>{3}8</a>{4}:</a></font>",First,Previous,numberStr,Next,Last);

   StringBuilder sb = new StringBuilder();//HTML字符串

   sb.AppendFormat("<table style = 'font-size:12px' border='0' cellpadding='0' cellspacing='0' width='100%'> \n " +

    "<tr>\n" +

    "<td width='30%' align='left'>{0}</td>\n" +

    "<td width='50%' align='right'>{1}</td>\n" +

    "<td width='20%' align='right'><input type='text' name='T1' size='4' style='border-bottom:solid 1pt gray;border-top :solid 1pt gray; border-left:solid 1pt gray;border-right:solid 1pt gray;'> \n    <input type='button' name='B1' size='6' value=go style='border-bottom:solid 1pt gray;border-top :solid 1pt gray; border-left:solid 1pt gray;border-right:solid 1pt gray;' onclick='go(T1,{2})'></td>\n" +

    "</tr>\n" +

    "</table>",leftInfo,

    centerInfo.ToString(),
      
    this.Pages);
   
   writer.Write(sb.ToString());

  }

 

  protected override void OnPreRender(EventArgs e)

  {
//注册控件
   base.OnPreRender (e);

   if(!Page.IsClientScriptBlockRegistered("WEREW-332DFAF-FDAFDSFDSAFD"))

   {

    Page.RegisterClientScriptBlock("WEREW-332DFAF-FDAFDSFDSAFD",SCRIPTSTRING);

   }

  }
 }


===============================================================================
以下的JS代码,和分页控件放在同一个页面上
<script language="javascript">

   function go(ctrl,max)

   {

       if(ctrl.value >= 1 && ctrl.value<= max)

       {

          var url;
          var index;         
          var s;

          url = location.href;

          index = url.indexOf('?');

          if(index == -1)

   {              
    url=url+"?";
   }

   else

   {
   
    s=url.substring(url.indexOf("?"));    
    s=s.toLowerCase();
    if(s.indexOf("currentpage")==-1)      //不存在currentpage变量
    {
     url=url+"&";
    }
    else         //多个参数时,currentpage必须放在最右边才能正确
    {
     url=url.substring(0,url.indexOf("?"))+s.substring(0,s.indexOf("currentpage"));
    }
    
   }
          location.href = url + "currentPage=" + ctrl.value;       }

       else

       {

          alert("您输入的页码必须是符合页面要求的数字,最大值是:" + max);

          return false;

       }

   }
  </script>

posted on 2006-11-30 16:02  discoverx  阅读(323)  评论(0编辑  收藏  举报