asp.net Repeater之非常好的数据分页

  using System;

  using System.Collections.Generic;

  using System.ComponentModel;

  using System.Text;

  using System.Web;

  using System.Web.UI;

  using System.Web.UI.WebControls;

  using System.Collections;

  #region Assembly Resource Attribute

  [assembly: System.Web.UI.WebResource("WebControlLibrary.Js.AjaxHelper.js", "text/javascript")]

  [assembly: System.Web.UI.WebResource("WebControlLibrary.img.bg_pager.png", "image/gif")]

  #endregion

  namespace WebControlLibrary

  {

  ///

  /// Repeater分页控件可进行Ajax分页(但Ajax分页不能传参数)

  ///

  [DefaultProperty("Text")]

  [ToolboxData("<{0}:RepeaterPages runat=server>")]

  public class RepeaterPages : WebControlLibrary.Repeater

  {

  //

  /// 构造函数

  ///

  public RepeaterPages()

  : base()

  {

  }

  ///

  /// 下一页分页文本内容

  ///

  private string _NextPage = ">";

  ///

  /// 下一页标记

  ///

  [Bindable(false), Description("下一页标记所显示的文本内容")]

  public string NextPage

  {

  get { return _NextPage; }

  set { _NextPage = value; }

  }

  ///

  /// 下一页标记

  ///

  private string _LastPage = "<";

  ///

  /// 上一页标记

  ///

  [Bindable(false), Description("上一页标记所显示的文本内容")]

  public string LastPage

  {

  get { return _LastPage; }

  set { _LastPage = value; }

  }

  ///

  /// 总记录数属性

  ///

  private int _RecordCount;

  ///

  /// 页面分页大小属性

  ///

  private int _PageSize;

  ///

  /// 当前页数

  ///

  private int _CurrentPage;

  ///

  /// 返回总页面

  ///

  private int _AllPages;

  ///

  /// 页面地址

  ///

  private string _Url;

  ///

  /// 传入Sql语句

  ///

  private string _SqlString;

  ///

  /// 每页显示页码数

  ///

  private int _PageNumberCount;

  ///

  /// 数据库中显示的字段列表

  ///

  private string _ShowTableCollName;

  ///

  /// 记录数属性

  ///

  [Bindable(true), Category("Appearance"), DefaultValue("0")]

  private int RecordCount

  {

  get

  {

  int IntTempValue = 0;

  if (Int32.TryParse(Sql_Function.SqlDb.RecordCounts(SqlString), out IntTempValue))

  return IntTempValue;

  else

  return 0;

  }

  set

  {

  _RecordCount = value;

  }

  }

  ///

  /// 每页分页大小属性

  ///

  [Bindable(true), Category("Appearance"), DefaultValue("50"), Description("每页分页大小属性")]

  public int PageSize

  {

  get

  {

  if (_PageSize == 0 || _PageSize.ToString() == "" || _PageSize > 200)

  {

  return 50;

  }

  else if (Sql_Function.PublicFunction.isNumber(_PageSize.ToString()))

  {

  return 50;

  }

  else if (_PageSize <= 0)

  {

  return 50;

  }

  else if (_PageSize > 200)

  {

  return 50;

  }

  else

  {

  return _PageSize;

  }

  }

  set { _PageSize = value; }

  }

  ///

  /// 每页显示的页码个数

  ///

  [Bindable(true), Category("Appearance"), DefaultValue("5"), Description("每页显示的页码个数")]

  public int PageNumberCount

  {

  get

  {

  if (_PageNumberCount == 0 || _PageNumberCount.ToString() == "" || _PageNumberCount > 30)

  {

  return 5;

  }

  else if (Sql_Function.PublicFunction.isNumber(_PageNumberCount.ToString()))

  {

  return 5;

  }

  else if (_PageNumberCount <= 0)

  {

  return 5;

  }

  else if (_PageNumberCount > 30)

  {

  return 5;

  }

  else

  {

  return _PageNumberCount;

  }

  }

  set { _PageNumberCount = value; }

  }

  ///

  /// 返回总页数

  ///

  private int AllPages

  {

  get

  {

  int totalpage;

  if (RecordCount / PageSize == 0)

  {

  totalpage = RecordCount / PageSize;

  }

  else

  {

  totalpage = RecordCount / PageSize + 1;

  }

  return totalpage;

  }

  set { _AllPages = value; }

  }

  ///

  /// 当前页数

  ///

  private int CurrentPage

  {

  get

  {

  string RequestCurrentPage = System.Web.HttpContext.Current.Request["page"];

  #region 计算当前页数

  int result_currentpage = 1;

  if (RequestCurrentPage == null)///指地址栏中没有PAGE字符

  {

  result_currentpage = 1;

  }

  if (RequestCurrentPage != null)

  {

  if (RequestCurrentPage.Length > 10 || RequestCurrentPage.Length < 1)///防止Convert.ToInt32抛出异常或者防止地址栏所得到的page=这样的值

  {

  result_currentpage = 1;

  }

  else///是数值

  {

  if (Sql_Function.PublicFunction.isNumber(RequestCurrentPage))// 判断是否为数值

  {

  result_currentpage = 1;

  }

  else

  {

  if (Convert.ToInt32(RequestCurrentPage) > AllPages)///是否大于总页数

  {

  result_currentpage = 1;

  }

  else

  {

  if (Convert.ToInt32(RequestCurrentPage) <= 1)///是否小于页数1

  {

  result_currentpage = 1;

  }

  else

  {

  result_currentpage = Convert.ToInt32(RequestCurrentPage);

  }

  }

  }

  }

  }

  else

  {

  result_currentpage = 1;

  }

  return result_currentpage;

  #endregion

  }

  set { _CurrentPage = value; }

  }

  ///

  /// 用于分页所显示URL参数链接(完整的URL参数)

  ///

  [Bindable(false), Description("用于分页所显示URL参数链接(完整的URL参数)")]

  public string Url

  {

  get

  {

  if (_Url == null || _Url.ToString() == "")

  {

  return "?";

  }

  else

  {

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

  {

  string a = "?";

  a = a + _Url;

  return a;

  }

  else if (Convert.ToInt32(StringFindCount(_Url, "?")) > 1)//如果含有多个?号

  {

  return "?";

  }

  else

  {

  return _Url;

  }

  }

  }

  set { _Url = value; }

  }

  ///

  /// 传入Sql语句

  ///

  [Bindable(false), Description("传入统计记录总数的Sql语句 例如:select count(id) from tablename")]

  public string SqlString

  {

  get

  {

  if (_SqlString == null || _SqlString.ToString() == "" || _SqlString.Length < 15)

  {

  return "";

  }

  else

  {

  return _SqlString;

  }

  }

  set { _SqlString = value; }

  }

  ///

  /// 数据库中显示的字段列表

  ///

  [Bindable(false), Description("数据库中显示的字段列表 多个字段中间用逗号隔开")]

  public string ShowTableCollName

  {

  get

  {

  if (_ShowTableCollName == null || _ShowTableCollName == "")

  {

  return "*";

  }

  else

  {

  return _ShowTableCollName;

  }

  }

  set { _ShowTableCollName = value; }

  }

  ///

  /// 分页信息前和尾所包含的HTML代码

  ///

  private string _PageBeforeHtml;

  ///

  /// 分页信息前和尾所包含的HTML代码

  ///

  private string _PageLastHtml;

  ///

  /// 分页信息前和尾所包含的HTML代码

  ///

  [Bindable(false), Description("分页信息前和尾所包含的HTML代码 ")]

  public string PageBeforeHtml

  {

  get { return _PageBeforeHtml; }

  set { _PageBeforeHtml = value; }

  }

  ///

  /// 分页信息前和尾所包含的HTML代码

  ///

  [Bindable(false), Description("分页信息前和尾所包含的HTML代码 ")]

  public string PageLastHtml

  {

  get { return _PageLastHtml; }

  set { _PageLastHtml = value; }

  }

  ///

  /// 调用的样式

  ///

  private string _Css = "";

  ///

  /// 调用的样式

  ///

  [Bindable(false), Category("Behavior"), DefaultValue("默认样式"), TypeConverter(typeof(WebPageCssSelect)), Description("分页样式")]

  public string Css

  {

  get { return _Css; }

  set { _Css = value; }

  }

  ///

  /// 是否为分页加上快捷输入框输入数值回车导航分页

  ///

  private bool _SelectPage = true;

  ///

  /// 是否为分页加上快捷输入框输入数值回车导航分页

  ///

  [Bindable(false), Description("是否为分页加上快捷输入框输入数值回车导航分页")]

  public bool SelectPage

  {

  get { return _SelectPage; }

  set { _SelectPage = value; }

  }

  ///

  /// 分页显示对齐方式

  ///

  private string _AlignSorts = "right";

  ///

  /// 分页显示对齐方式

  ///

  [Bindable(false), DefaultValue("right"), TypeConverter(typeof(WebPageAlign)), Description("分页显示对齐方式")]

  public string AlignSorts

  {

  get { return _AlignSorts; }

  set { _AlignSorts = value; }

  }

  ///

  /// 分页Js分页函数调用名称

  ///

  private string _JsFunctionName = "_doPostBack";

  ///

  /// 分页Js分页函数调用名称

  ///

  [Bindable(false), Description("分页Js分页函数调用名称")]

  public string JsFunctionName

  {

  get

  {

  if (_JsFunctionName == null || _JsFunctionName.Replace("'", "").Replace(""", "") == "")

  {

  return "_doPostBack";

  }

  else

  {

  return _JsFunctionName;

  }

  }

  set { _JsFunctionName = value; }

  }

  ///

  /// 是否启用简洁分页样式

  ///

  private bool _AutoPageConcise = false;

  ///

  /// 是否启用简洁分页样式

  ///

  [Bindable(false), Description("是否启用简洁分页样式")]

  public bool AutoPageConcise

  {

  set { _AutoPageConcise = value; }

  get { return _AutoPageConcise; }

  }

  ///

  /// 是否允许无刷新调用分页

  ///

  private bool _BoolIsAutoAjaxPage = false;

  ///

  /// 是否允许无刷新调用分页

  ///

  [Bindable(false), Description("是否允许无刷新调用分页")]

  public bool BoolIsAutoAjaxPage

  {

  get { return _BoolIsAutoAjaxPage; }

  set { _BoolIsAutoAjaxPage = value; }

  }

  ///

  /// 无刷新调用的页面或自定义控件页面ascx分页信息页 完整路径

  ///

  private string _AutoAjaxPageUrl = "";

  ///

  /// 无刷新调用的页面或自定义控件页面ascx分页信息页 完整路径

  ///

  [Bindable(false), Description("无刷新调用的页面或自定义控件页面ascx分页信息页 完整路径")]

  public string AutoAjaxPageUrl

  {

  get { return _AutoAjaxPageUrl; }

  set { _AutoAjaxPageUrl = value; }

  }

  ///

  /// 用于无刷新中间级异步调用分页信息页面的中间层页面地址

  ///

  private string _AutoAjaxReturnPageUrl = "";

  ///

  /// 用于无刷新中间级异步调用分页信息页面的中间层页面地址

  ///

  [Bindable(false), Description("用于无刷新中间级异步调用分页信息页面的中间层页面地址")]

  public string AutoAjaxReturnPageUrl

  {

  get { return _AutoAjaxReturnPageUrl; }

  set { _AutoAjaxReturnPageUrl = value; }

  }

  ///

  /// 无刷新调用的页面所返回的控件ID

  ///

  private string _AutoAjaxPageWebControl = "";

  ///

  /// 无刷新调用的页面所返回的控件ID

  ///

  [Bindable(false), Description("无刷新调用的页面所返回的控件ID")]

  public string AutoAjaxPageWebControl

  {

  get { return _AutoAjaxPageWebControl; }

  set { _AutoAjaxPageWebControl = value; }

  }

  ///

  /// 输出html,在浏览器中显示控件

  ///

  /// 要写出到的 HTML 编写器

  protected override void Render(HtmlTextWriter output)

  {

  base.Render(output);

  if (!this.Page.ClientScript.IsClientScriptBlockRegistered(this.Page.GetType(), "AjaxPageJs"))

  {

  output.Write("

  

  ";

  }

  pagestr += "