小巧精致的ASP.Net分页控件
- using System;
- using System.Data;
- using System.Configuration;
- using System.Collections;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- using System.Text;
- using System.Text.RegularExpressions;
- namespace MyTest
- {
- public partial class PageShow : System.Web.UI.UserControl
- {
- #region 私有成员
- /// <summary>
- /// 当前页的页码的背景色
- /// </summary>
- private string _color = "#a8ceff";
- /// <summary>
- /// 总记录数
- /// </summary>
- private int _recTotal = 0;
- /// <summary>
- /// 每页显示的记录数, 默认为10
- /// </summary>
- private int _pageSize = 10;
- /// <summary>
- /// 当前页的页码左右要显示的页码的个数, 默认7个
- /// </summary>
- private int _pageShowNum = 7;
- #endregion
- #region 公共属性
- /// <summary>
- /// 设置或读取总记录数,不能小于0
- /// </summary>
- public int RecTotal
- {
- get { return _recTotal; }
- set
- {
- if (value > 0)
- _recTotal = value;
- }
- }
- /// <summary>
- /// 设置或读取每页显示的记录数,不能小于0
- /// </summary>
- public int PageSize
- {
- get { return _pageSize; }
- set
- {
- if (value > 0)
- _pageSize = value;
- }
- }
- /// <summary>
- /// 读取总页数
- /// </summary>
- public int PageTotal
- {
- get {
- int total = (int)Math.Ceiling((double)_recTotal / _pageSize);
- return total;
- //return PageTotal;
- }
- }
- /// <summary>
- /// 读取当前要显示第几页,注:页码传递方式是Request.QueryString["page"]
- /// </summary>
- public int PageIndex
- {
- get
- {
- int pageIndex = 1;
- string tmp = Request.QueryString["page"];
- if (tmp != null && tmp != string.Empty)
- {
- if (tmp == "end")
- pageIndex = PageTotal;
- else
- {
- try
- {
- pageIndex = int.Parse(tmp);
- if (pageIndex > PageTotal)
- pageIndex = PageTotal;
- else if (pageIndex < 1)
- pageIndex = 1;
- }
- catch (Exception)
- {
- pageIndex = 1;
- }
- }
- }
- return pageIndex;
- }
- }
- /// <summary>
- /// 设置或读取当前页的页码的背景色,默认为#a8ceff
- /// </summary>
- public string Color
- {
- get { return _color; }
- set { _color = value; }
- }
- /// <summary>
- /// 当前页的页码左右要显示的页码的个数,默认7个
- /// </summary>
- public int PageShowNum
- {
- get { return _pageShowNum; }
- set { _pageShowNum = value; }
- }
- #endregion
- /// <summary>
- /// 重写Render事件,发送生成的分页Url
- /// </summary>
- /// <param name="writer"></param>
- protected override void Render(HtmlTextWriter writer)
- {
- if (PageTotal <= 1)
- {//少于1页时,不显示
- return;
- }
- StringBuilder writeStr = new StringBuilder();
- #region 控件使用的CSS代码
- writeStr.Append(@"
- <style type='text/css'>.page_total {background-color: #F5FBFF; border: 1px solid #86B9D6; border-right: 0px solid #86B9D6; font-weight: bold; }
- .page_pages {background-color: #F5FBFF; border: 1px solid #86B9D6; margin-right:1px; vertical-align: middle; font-weight: bold; }
- .page_num {background-color: #FFFFFF; border: 1px solid #DEDEB8; margin-right:1px; vertical-align: middle; }
- a:hover.page_num {background-color: #F5FBFF; border: 1px solid #86B9D6; text-decoration: none; }
- .page_curpage { margin-right:1px; border: 1px solid #DEDEB8; vertical-align: middle; background-color: #FFFF80; color: #92A05A; font-weight: bold; }
- .page_bar { margin: 1px 0px; clear: both; }
- .page_bar a { float: left; padding: 1px 4px; font-size: 12px; text-decoration: none; }
- .page_redirect { background-color: #FFFFFF; border: 1px solid #DEDEB8; margin-right:1px; font-size: 12px !important; font-size: 13px; }
- a:hover.page_redirect { background-color: #F5FBFF; border: 1px solid #86B9D6; text-decoration: none; }
- </style>");
- #endregion
- writeStr.Append("<table cellspacing='0' cellpadding='0' align='left' style='clear: both;'>");
- writeStr.Append("<tr><td valign='bottom'>");
- writeStr.Append("<div class='page_bar'>");
- writeStr.Append("<a class='page_total' title='记录总数'> " + _recTotal.ToString() + " </a>");
- writeStr.Append("<a class='page_pages' title='当前页/总页数'> " + PageIndex + "/" + PageTotal + " </a>");
- string url = GetUrl().ToLower();//取得当前页面的完整URL
- string tmp;
- //没有page时,加一个page, 方便后面替换
- if (url.IndexOf("page=") < 0)
- {
- url = url + ((url.IndexOf("?") < 0) ? "?" : "&") + "page=1";
- }
- //设定正则表达式,用于替换url中的page=字符串
- Regex my = new Regex(@"(page=[0-9]*(end)*)", RegexOptions.IgnoreCase);
- #region 如果当前是指定显示个数的页码之后的页时,加上到第一页和上一页的链接(此时页面不会显示页码1)
- if (PageIndex > _pageShowNum + 1)
- {
- //第一页
- tmp = my.Replace(url, "page=1");
- writeStr.Append("<a class='page_redirect' href='" + tmp + "' title='第一页'>|<</a>");
- //上一页
- tmp = my.Replace(url, "page=" + (PageIndex - 1).ToString());
- writeStr.Append("<a class='page_redirect' href='" + tmp + "' title='上一页'><</a>");
- }
- #endregion
- //总共显示( _pageShowNum * 2 + 1 )页的链接,从当前页往前_pageShowNum页,往前不足_pageShowNum页时,后面的页数增加.
- int countPage = 0;//用于统计是否超过( _pageShowNum * 2 + 1 )页
- int loopStart;
- //当总页数大于( _pageShowNum * 2 + 1 ),且当前页为最后_pageShowNum页时,保持页面页码数不少于( _pageShowNum * 2 + 1 )
- if (PageTotal - PageIndex >= _pageShowNum)
- {
- // 总页数 减 当前页码 大于等于左边要显示的个数时,循环 从当前页码 大于等于左边要显示的个数 开始
- loopStart = PageIndex - _pageShowNum;
- }
- else
- {
- loopStart = PageTotal - _pageShowNum * 2;// PageIndex - _pageShowNum - (_pageShowNum - PageTotal + PageIndex) ==> PageTotal - _pageShowNum * 2
- }
- for (int loop = loopStart; loop <= PageTotal; loop++)
- {
- if (loop < 1)
- continue;
- else
- countPage++;
- #region 已经显示了( _pageShowNum * 2 + 1 )页 并且 当前页数小于等于总页数时,加上到最末页和上一页的链接并退出循环(不会显示最后1页的页码时)
- if (countPage > (_pageShowNum * 2 + 1) && loop <= PageTotal)
- {
- //下一页
- tmp = my.Replace(url, "page=" + (PageIndex + 1).ToString());
- writeStr.Append("<a class='page_redirect' href='" + tmp + "' title='下一页'>></a>");
- //最终页
- tmp = my.Replace(url, "page=" + PageTotal.ToString());// my.Replace(url, "page=end");
- writeStr.Append("<a class='page_redirect' href='" + tmp + "' title='最末页'>>|</a>");
- break;
- }
- #endregion
- #region 添加当前循环页的链接
- if (loop == PageIndex)
- {//当前页
- writeStr.Append("<a class='page_curpage' style='background-color:" + _color + "'>" +
- loop.ToString() + "</a>");// style='width:" + (loop > 99 ? (width + 5).ToString() : width.ToString()) + "px;'
- }
- else
- {
- //计算下一页的链接
- tmp = my.Replace(url, "page=" + loop.ToString());
- writeStr.Append("<a class='page_num' href='" + tmp + "' title='第" +
- loop.ToString() + "页''>" + loop.ToString() + "</a>");
- }
- #endregion
- }
- writeStr.Append("</div></td></tr></table>");
- writer.Write(writeStr.ToString());
- }
- /// <summary>
- /// 获取当前页面的完整URL地址信息
- /// </summary>
- /// <returns></returns>
- public string GetUrl()
- {
- string url;
- url = System.Web.HttpContext.Current.Request.ServerVariables["SERVER_NAME"];
- if (System.Web.HttpContext.Current.Request.ServerVariables["SERVER_PORT"] != "80")
- url += ":" + System.Web.HttpContext.Current.Request.ServerVariables["SERVER_PORT"];
- //strTemp = strTemp & CheckStr(Request.ServerVariables("URL"))
- url += System.Web.HttpContext.Current.Request.ServerVariables["SCRIPT_NAME"];
- if (!(System.Web.HttpContext.Current.Request.QueryString == null || System.Web.HttpContext.Current.Request.QueryString.ToString() == ""))
- {
- url += "?" + System.Web.HttpContext.Current.Request.QueryString.ToString();
- }
- string https = System.Web.HttpContext.Current.Request.ServerVariables["HTTPS"];
- if (https == null || https == string.Empty || https == "off")
- {
- url = "http://" + url;
- }
- else
- {
- url = "https://" + url;
- }
- return url;
- }
- }
- }
具体的控件代码如下,在你的VS.Net里新建一个用户控件,然后把下面的代码拷贝进去就可以用了,最下面是使用示