生成分页码的代码

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.RegularExpressions;
using System.Text;

namespace manage.Controls
{
    public partial class Pages : System.Web.UI.UserControl
    {
        private int _CuttentPage;
        private int _NumCount;
        private int _PageSize=1;
        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;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
          
        }
        /// <summary>
        /// 根据当前的页数显示的当前页的底部分页
        /// </summary>
        /// <param name="current"></param>
        /// <param name="size"></param>
        /// <param name="zong"></param>
        /// <returns></returns>
        public string GetPage(int current, int size, int zong,string url)
        {
            string urltemp = url.IndexOf("page=")>=0?(Regex.Replace(url,"page=([^&]*)","page={0}")):(url.IndexOf("?")>=0?(url+"&page={0}"):(url+"?page={0}"));

            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)+1); 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();
        }
    }
}

你可能感兴趣的:(分页)