• 最新page页码生成控件代码


    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=\" 30%\"><input type=\"text\" id=\"Pages1_TbPageGo\"  onKeyUp=\"this.value=this.value.replace(/[^\\d]+/,'')\" style=\" 33px\"/><input id=\"BtGo\" style=\" 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\"><tr><td width=\"100%\">{0}</td></tr></table>";
                output.Write(string.Format(tempData, GetPage(CurrentPage,PageSize,NumCount)));
            }
        }
    }

  • 相关阅读:
    英语八大时态
    linux yum被占用
    文件上传ajaxfileupload.js插件
    jquery.qrcode.js生成二维码
    判断手机,pc方式登录
    点击小图轮播,点击显示大图
    图片预览功能
    Java Hash表 数据结构
    Java Tree 树 数据结构
    Java Queue 队列
  • 原文地址:https://www.cnblogs.com/bestsaler/p/1835591.html
Copyright © 2020-2023  润新知