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.Text.RegularExpressions;
using System.Collections;
namespace WebPages
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:Pages runat=\"server\"></{0}:Pages>")]
    public class Pages : WebControl
    {
        private int _CuttentPage;
        private int _NumCount;
        private int _PageSize = 1;
        private string _Url = "";
        private string _RewriteFormat = "";
        private string _RewriteRegex = "";
        private Dictionary<string, string> _regs;
        private string _CurrentCss;
        public int CurrentPage
        {
            set
            {
                if (Regex.IsMatch(Convert.ToString(value), "^\\d+$"))
                {
                    this._CuttentPage = Convert.ToInt32(value);
                }
                else
                {
                    this._CuttentPage = 0;
                }
            }
            get
            {
                return this._CuttentPage;
            }
        }
        public int NumCount
        {
            set
            {
                if (Regex.IsMatch(Convert.ToString(value), "^\\d+$"))
                {
                    this._NumCount = Convert.ToInt32(value);
                }
                else
                {
                    this._NumCount = 0;
                }
            }
            get
            {
                return this._NumCount;
            }
        }
        public int PageSize
        {
            set
            {
                if (Regex.IsMatch(Convert.ToString(value), "^\\d+$"))
                {
                    this._PageSize = Convert.ToInt32(value);
                }
                else
                {
                    this._PageSize = 1;
                }
            }
            get
            {
                return this._PageSize;
            }
        }
        //地址
        public string Url
        {
            set
            {
                _Url = value;
            }
            get
            {
                if (string.IsNullOrEmpty(_Url))
                {
                    try
                    {
                        _Url = this.Parent.Page.Request.RawUrl.ToString();
                    }
                    catch
                    {

                        _Url = "";
                    }
                 
                }
                return this._Url;
            }
        }
        public string RewriteFormat
        {
            set { this._RewriteFormat = value; }
            get { return this._RewriteFormat; }
        }
        public string RewriteRegex
        {
            set { _RewriteRegex = value; }
            get { return this._RewriteRegex; }
        }
        public string CurrentCss
        {
            set { _CurrentCss = value; }
            get {
                if (string.IsNullOrEmpty(_CurrentCss))
                    _CurrentCss = "";
                return this._CurrentCss; }
        }
        public Dictionary<string, string> Regs
        {
            set {
                this._regs = value;
            }
            get {
                if (_regs == null) {
                    _regs = new Dictionary<string, string>();              
                }                
                return _regs;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                String s = (String)ViewState["Text"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["Text"] = value;
            }
        }


        protected string getUrl(string url)
        {
            MatchCollection mats = Regex.Matches(url, "(?<first>[\\w]*)=(?<two>[^&]*[\u4e00-\u9fa5]+[^&]*)", RegexOptions.IgnoreCase | RegexOptions.Multiline);
            foreach (Match mat in mats)
            {
          
                url = url.Replace(mat.Groups["first"].Value + "=" + mat.Groups["two"].Value, mat.Groups["first"].Value + "=" + HttpUtility.UrlEncodeUnicode(mat.Groups["two"].Value.Trim()));
             }
            return url;
        }
        /// <summary>
        /// 根据当前的页数显示的当前页的底部分页
        /// </summary>
        /// <param name="current"></param>
        /// <param name="size"></param>
        /// <param name="zong"></param>
        /// <returns></returns>
        protected string GetPage(int current, int size, int zong)
        {
            string url = this.Url;
            url = getUrl(url).ToLower();
          
            string urltemp ="";
            urltemp = url.IndexOf("page=") >= 0 ? (Regex.Replace(url, "page=([^&]*)", "page={0}",RegexOptions.Multiline|RegexOptions.Singleline|RegexOptions.IgnoreCase)) : (url.IndexOf("?") >= 0 ? (url + "&page={0}") : (url + "?page={0}"));
            if(this.RewriteFormat.Length>0)
            {
                Regex reg = new Regex(RewriteRegex, RegexOptions.IgnoreCase);
                if (reg.IsMatch(urltemp))
                {
                    urltemp = reg.Replace(urltemp, RewriteFormat);
                }
            }        
            int pagecount = zong % size == 0 ? (zong / size) : (zong / size + 1);
            int num = current / 10;

            System.Text.StringBuilder sball = new System.Text.StringBuilder("");
            int xian = current / 10 + 1;

            int before = current % 10 == 0 ? (current / 10) : (current / 10);
            sball.AppendFormat("<a href='" + string.Format(urltemp, current - 1 < 1 ? ("1") : ((current - 1).ToString())) + "'>上一页</a>&nbsp;");
            for (int i = ((before * 10 - 5) > 0 ? (before * 10 - 5) : (1)); i <= (before * 10 == 0 ? (1) : (before * 10-1)); i++)
            {
                if ((before * 10 - 5) < 0)
                    continue;
                if ((i+1) == current)
                    sball.AppendFormat("<a href='" + string.Format(urltemp, (i + 1).ToString()) + "' class=\"{1}\">{0}</a>&nbsp;", (i + 1).ToString(), CurrentCss);
                else
                    sball.AppendFormat("<a href='" + string.Format(urltemp, (i + 1).ToString()) + "'>{0}</a>&nbsp;", (i + 1).ToString());
                sball.Append("&nbsp;");
            }
            for (int i = ((xian - 1) * 10 > pagecount ? (pagecount) : ((xian - 1) * 10)); i < (xian * 10 < pagecount ? (xian * 10) : (pagecount)); i++)
            {

                if (i == 0)
                    sball.AppendFormat("<a href='" + string.Format(urltemp, (i + 1).ToString()) + "'>").Append((i + 1)).Append("</a>&nbsp;");
                else if ((i+1) == current)
                    sball.AppendFormat("<a href='" + string.Format(urltemp, (i + 1).ToString()) + "' class=\"{1}\">{0}</a>&nbsp;", (i + 1).ToString(), CurrentCss);

                else
                    sball.Append("&nbsp;<a href='" + string.Format(urltemp, (i + 1).ToString()) + "'>").Append((i + 1)).Append("</a>&nbsp;");

            }
            sball.Append("<a href='" + string.Format(urltemp, (current + 1 > pagecount ? (pagecount.ToString()) : ((current + 1).ToString()))) + "'>下一页</a>");
            //sball.Append("&nbsp;共").Append(zong.ToString()).Append("条&nbsp;当前第&nbsp;").Append((current).ToString()).Append("&nbsp;页").Append("&nbsp;共&nbsp;").Append(pagecount).Append("&nbsp;页");
            return sball.ToString();
          
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            //<td style=\"width: 30%\"><input type=\"text\" id=\"Pages1_TbPageGo\"  onKeyUp=\"this.value=this.value.replace(/[^\\d]+/,'')\" style=\"width: 33px\"/><input id=\"BtGo\" style=\"width: 34px\" type=\"button\" value=\"GO\" onClick=\"document.location.href=document.location.href.indexOf('page=')>=0?(document.location.href.replace(/page=([^&]*)/,'page='+document.getElementById('Pages1_TbPageGo').value)):(document.location.href.indexOf('?')>=0?(document.location.href+'&page='+document.getElementById('Pages1_TbPageGo').value):(document.location.href+'?page='+document.getElementById('Pages1_TbPageGo').value))\"/></td>
            string tempData = "<table border=\"0\" width=\"100%\" height=\"30px\" style=\"margin:0px\" class=\"pagers\"><tr><td width=\"100%\" style=\"padding-top:5px\">{0}{1}</td></tr></table>";
            output.Write(string.Format(tempData, GetPage(CurrentPage, PageSize, NumCount), "<style type=\"text/css\">.pagers{ background:#F2F7FD; border-top:#B1E6EC 1px solid; border-bottom:#B1E6EC 1px solid; margin-top:10px;  text-align:center; padding:12px 0;color:#000; font-size:12px } .pagers a{  border:#CBCBCB 1px solid; padding:2px 5px 0px 5px; background:#fff;text-decoration: none; } .pagers a:hover{ border:#58C9D6 1px solid; background:#D8EEFB} </style>"));
        }
    }
}

posted on 2009-06-18 12:59 sanmao 阅读(93) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航:
 

常用链接

留言簿(5)

随笔分类

随笔档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜