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;

public partial class Controls_PageControl : System.Web.UI.UserControl
{
    private string _UrlFormat;
    private int _PageSize;
    private int _RecordCount;
    private int _PageCount = 5;
    private int _PageIndex;
    private int flag = 0;

    /// <summary>
    /// 连接格式
    /// </summary>
    public string UrlFormat
    {
        get
        {
            return _UrlFormat;
        }
        set
        {
            _UrlFormat = value;
        }
    }

    /// <summary>
    /// 每页显示记录数
    /// </summary>
    public int PageSize
    {
        get
        {
            return _PageSize;
        }
        set
        {
            _PageSize = value;
        }
    }

    /// <summary>
    /// 当前页码
    /// </summary>
    public int PageIndex
    {
        get
        {
            string Pageindex = HttpContext.Current.Request.QueryString["page"];
            if (Pageindex != null)
            {
                return int.Parse(Pageindex);
            }
            return 1;
        }
    }

    /// <summary>
    /// 总记录数
    /// </summary>
    public int RecordCount
    {
        get
        {
            return _RecordCount;
        }
        set
        {
            _RecordCount = value;
        }
    }

    /// <summary>
    /// 两边显示个数
    /// </summary>
    public int PageCount
    {
        get
        {
            return _PageCount;
        }
        set
        {
            _PageCount = value;
        }
    }

    protected override void Render(HtmlTextWriter writer)
    {
        if (PageSize == 0)
            PageSize = int.Parse(ConfigurationSettings.AppSettings["PageSize"]);
        PageCount = System.Convert.ToInt32(Math.Ceiling((Decimal)RecordCount / PageSize));//获取页数
        if (RecordCount == 0)
            return;
        int SumPage = (RecordCount + PageSize - 1) / PageSize;//页数?

        flag = (PageIndex - 1) / 10;
        string tmp = "<a href=\"" + UrlFormat + "\">[{0}]</a>";
        StringBuilder sb = new StringBuilder(string.Format("页次:{0}/{1}  每页:{2}  共计:{3} 条 ", PageIndex, SumPage, PageSize, RecordCount));
        if (PageIndex > 1)
        {
            sb.Append(string.Format("<a href=\"" + UrlFormat + "\">首页</a> ", 1));
            sb.Append(string.Format(" <a href=\"" + UrlFormat + "\">上一页</a> ", PageIndex - 1));
        }
        if (this.PageIndex > 10)
        {
            sb.Append(string.Format("<a href=\"" + UrlFormat + "\"><<</a> ", 10 * flag));
        }
        int start, end;
        start = 10 * flag + 1;
        if (10 * (flag + 1) > PageCount)
        {
            end = PageCount;
        }
        else
        {
            end = 10 * (flag + 1);
        }

        for (int i = start; i <= end; i++)
        {
            if (i == PageIndex)
            {
                sb.Append(" <strong>" + PageIndex.ToString() + "</strong> ");
            }
            else
            {
                sb.Append(string.Format(tmp, i));
            }
            sb.Append("&nbsp;");
        }
        if (PageCount > 10 * (flag + 1))
        {
            sb.Append(string.Format("<a href=\"" + UrlFormat + "\">>></a> ", 10 * (flag + 1) + 1));
        }
        if (PageIndex < SumPage)
        {
            sb.Append(string.Format(" <a href=\"" + UrlFormat + "\">下一页</a> ", PageIndex + 1));
            sb.Append(string.Format(" <a href=\"" + UrlFormat + "\">尾页</a>", SumPage));
        }

        writer.Write(sb.ToString());
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }
}