页码生成控件|c#源码

页码生成控件|c#源码

[assembly: TagPrefix("WebPages", "WebPages")]   //XXX为自定义控件的名称空间,YYY为你所想要得名字

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;

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 = "";
        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; }
        }
       

        [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 GetRightRegexUrl(string url)
        {
            string[,] tempStrArr = new string[12,2];
            tempStrArr[0, 0] = "\\";
            tempStrArr[0, 1] = "\\\\u005C";
            tempStrArr[1, 0] = "$";
            tempStrArr[1, 1] = "\\\\u0024";
            tempStrArr[2, 0] = "^";
            tempStrArr[2, 1] = "\\\\u005E";
            tempStrArr[3, 0] = "{";
            tempStrArr[3, 1] = "\\\\u007B";
            tempStrArr[4, 0] = "[";
            tempStrArr[4, 1] = "\\\\u005B";
            tempStrArr[5, 0] = "(";
            tempStrArr[5, 1] = "\\\\u0028";
            tempStrArr[6, 0] = "|";
            tempStrArr[6, 1] = "\\\\u007C";
            tempStrArr[7, 0] = ")";
            tempStrArr[7, 1] = "\\\\u0029";
            tempStrArr[8, 0] = "*";
            tempStrArr[8, 1] = "\\\\u002A";
            tempStrArr[9, 0] = "+";
            tempStrArr[9, 1] = "\\\\u002B";
            tempStrArr[10, 0] = "?";
            tempStrArr[10, 1] = "\\\\\\\\u003F";
            tempStrArr[11, 0] = ".";
            tempStrArr[11, 1] = "\\\\\\\\u002E";
            for (int i = 0; i < 12; i++)
            {
                url = url.Replace(tempStrArr[i,0],tempStrArr[i,1]);              
            }
            return url;
        }
        protected string getUrl(string url)
        {
            MatchCollection mats = Regex.Matches(url, "(?<first>[\\w]*)=(?<two>[\\S\\s]*[\u4e00-\u9fa5]+[\\S\\s]*)", 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);
            string urltemp ="";
            urltemp = url.IndexOf("page=") >= 0 ? (Regex.Replace(url, "page=([^&]*)", "page={0}")) : (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("&nbsp;<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)); i++)
            {


                if ((before * 10 - 5) < 0)
                    continue;
                sball.AppendFormat("&nbsp;<a href='" + string.Format(urltemp, (i + 1).ToString()) + "'>{0}</a>", (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>");

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

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

            return sball.ToString();
          
        }

        protected override void RenderContents(HtmlTextWriter output)
        {

            string tempData = "<table border=\"1\" width=\"100%\" height=\"30px\" style=\"margin:0px\"><tr><td style=\"width: 89%\">{0}</td><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></tr></table>";
           // output.Write(Text);
            //<%=GetPage(this.CurrentPage, this.PageSize, this.NumCount, this.Request.Url.ToString())%>
            output.Write(string.Format(tempData, GetPage(CurrentPage,PageSize,NumCount)));
          
        }
    }
}

你可能感兴趣的:(页码生成控件|c#源码)