现在的位置: 首页 > 综合 > 正文

改写后的分页自定义控件

2013年05月18日 ⁄ 综合 ⁄ 共 5854字 ⁄ 字号 评论关闭

 using System;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Permissions;
using System.Collections.Specialized;
using Etmc.App.ETicket.Utility;

namespace Etmc.App.ETicket.WebControl
{
 /// <summary>
 /// 分页栏
 /// </summary>
 [
 AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal),
 AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal),
 ToolboxData("<{0}:PaginationBar runat=server></{0}:PaginationBar>")
 ]
 public class PaginationBar : System.Web.UI.WebControls.WebControl
 {
  public static int GetRequestPageS()
  {       
   HttpRequest request = HttpContext.Current.Request;

   if(request.Form["__VIEWSTATE"]!=null &&request.Form["__VIEWSTATE"].Trim() != "")
   {
    //IsPostBack
    return 1;
   }

   int intPage = PageUtility.GetRequestID("Page");   
   intPage = Math.Max(1,intPage);
   return intPage;      
  }
  public int GetRequestPage()
  {   
   return GetRequestPageS();   
  }
  private int _CurrentPage = 1;
  private int _PageSize = Constant.PAGE_SIZE;
  private int _ItemCount = 0;
  private string _UrlParameter = "";
  /// <summary>
  /// 当前页(1开始)
  /// </summary>
  public int CurrentPage
  {
   get
   {
    return this._CurrentPage;
   }
   set
   {
    this._CurrentPage = value;
   }
  }
  /// <summary>
  /// 页索引(0开始)
  /// </summary>
  public int PageIndex
  {
   get
   {
    return CurrentPage -1;
   }
   set
   {
    CurrentPage = value + 1;
   }
  }

  /// <summary>
  /// 每页显示数
  /// </summary>
  public int PageSize
  {
   get
   {
    return this._PageSize;
   }
   set
   {
    this._PageSize = value;
   }
  }

  /// <summary>
  /// 所有数据数
  /// </summary>
  public int ItemCount
  {
   get
   {
    return this._ItemCount;
   }
   set
   {
    this._ItemCount = value;
   }
  }

  /// <summary>
  /// 页面地址
  /// </summary>
  public string UrlParameter
  {
   get
   {
    return this._UrlParameter;
   }
   set
   {
    this._UrlParameter = value;
   }
  }

  private int _intPages = 0;  

  /// <summary>
  /// 赋值
  /// </summary>
  /// <param name="PageIndex">当前页</param>
  /// <param name="PageSize">每页显示数</param>
  /// <param name="ItemCount">所有数据数</param>
  /// <param name="UrlParameter">页面地址</param>
  public void SetValues(int PageIndex, int PageSize, int ItemCount, string UrlParameter)
  {
   this.PageIndex = PageIndex;
   this.PageSize = PageSize;
   this.ItemCount = ItemCount;
   this.UrlParameter = UrlParameter;
  }

  /// <summary>
  /// 将此控件呈现给指定的输出参数。
  /// </summary>
  /// <param name="output"> 要写出到的 HTML 编写器 </param>
  protected override void Render(HtmlTextWriter output)
  {   
   if (PageSize <= 0)
    PageSize = 1;

   _intPages = ItemCount / PageSize;
   if (ItemCount % PageSize != 0)
    _intPages++;         
   string strScript = @"
<script language='javascript'>
function PageBarSelect_onchange()
{
 var sel = document.getElementById('PageBarSelect');
 window.location.href = '" + UrlParameter + @"'+sel.options[sel.selectedIndex].value ;
}
function PageBarKeyDownEvent()
{
 if(window.event.keyCode == 13 || window.event.keyCode == 10)
 {
  PageBarClick();
  return;
 }
 if((window.event.keyCode>95 && window.event.keyCode<106)
  || (window.event.keyCode>47 && window.event.keyCode<59)
  || window.event.keyCode == 8
  || window.event.keyCode == 46
  || window.event.keyCode == 37
  || window.event.keyCode == 39)
 {
 }
 else
 {
  window.event.returnValue =0;
  return false;
 }
}
function PageBarClick()

 var i = document.getElementById('PateBarText').value ;
  
 if(PageBarValidate())
 {
  window.location.href=  '" + UrlParameter + @"' + i;
 }
 else
 { 
  return false;
 }
}
function PageBarValidate()
{
 var min=1;
 var max={2};
 var txt = document.getElementById(""PateBarText"");
 if( ( isNaN(Math.floor(txt.value) ))
  || ( Math.floor(txt.value)!=txt.value )
  || ( txt.value<min||txt.value>max ) )
 {
  alert(""请输入1到""+max);
  txt.focus();
  txt.select();
  return false;
 } 
 return true; 
}
</script>";

   strScript = strScript.Replace("{2}", this._intPages.ToString());

   output.Write(strScript);
   
   string lnkFirstPageHRef, lnkPrePageHRef, lnkNextPageHRef, lnkEndPageHRef;
   if (this._intPages > 1)
   {
    if (CurrentPage == 1)
    {
     lnkFirstPageHRef = "";
     lnkPrePageHRef = "";
    }
    else
    {
     lnkFirstPageHRef = "href=/"" + UrlParameter + "1" + "/"";
     lnkPrePageHRef = "href=/"" + UrlParameter + ( CurrentPage - 1).ToString() + "/"";
    }
    if (CurrentPage == this._intPages)
    {
     lnkNextPageHRef = "";
     lnkEndPageHRef = "";
    }
    else
    {
     lnkNextPageHRef = "href=/"" + UrlParameter + (CurrentPage+1).ToString() + "/"";
     lnkEndPageHRef = "href=/"" + UrlParameter + this._intPages.ToString() + "/"";
    }
   }
   else
   {
    lnkFirstPageHRef = "";
    lnkPrePageHRef = "";
    lnkNextPageHRef = "";
    lnkEndPageHRef = "";
   }

   output.Write("<div align=/"center/">");  
 
   output.Write(string.Format("总计 <span><strong>{0}</strong></span> 条,分 <span><strong>{1}</strong></span> 页显示,当前第<span><strong>{2}</strong></span>页&nbsp; ",
    ItemCount,
    _intPages,
    CurrentPage));

   output.Write(string.Format("<a {0}>首页</a> ", lnkFirstPageHRef));
   output.Write(string.Format("<a {0}>上一页</a> ", lnkPrePageHRef));
   output.Write(string.Format("<a {0}>下一页</a> ", lnkNextPageHRef));
   output.Write(string.Format("<a {0}>末页</a> &nbsp;", lnkEndPageHRef));
   if(_intPages<=50)
   {
    //多于50页没有下拉框
    output.Write("跳转至第");
    output.Write("<select name=/"PageBarSelect/" id=/"PageBarSelect/" onchange=/"return PageBarSelect_onchange()/">");
    for (int i = 1; i <= _intPages ; i++)
    {
     if (CurrentPage == i)
     {
      output.Write(string.Format("<option selected value=/"{0}/">{2}/{1}</option>", i.ToString(), _intPages, i));
     }
     else
     {
      output.Write(string.Format("<option value=/"{0}/">{2}/{1}</option>", i.ToString(), _intPages, i));
     }
    }
    output.Write("</select> 页&nbsp;");    
   }    
   
   output.Write(string.Format("<input id=/"PateBarText/" name=/"PateBarText/" type=/"text/" size=/"3/" value=/"/" onkeydown=/"return PageBarKeyDownEvent()/" maxlength=/"10/">&nbsp;",
    this.ClientID));
   output.Write("<INPUT language=/"javascript/" onclick=/"return PageBarClick()/" type=/"button/" value=/"go/">&nbsp;");
   
   output.Write("&nbsp;&nbsp;");
   
   output.Write("</div>");
  }
 }
}

抱歉!评论已关闭.