PAGINATION

来源:互联网 发布:三国志13卡顿优化补丁 编辑:程序博客网 时间:2024/05/21 17:55

using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Intiem.Info.Web
{
    [ToolboxData("<{0}:Pagination runat=server></{0}:Pagination>")]
    public class Pagination : WebControl, INamingContainer
    {
        public delegate void CommandEventHandler(object sender, CommandEventArgs e);

        public event CommandEventHandler CommandEvent;

        public int PageSize
        {
            get
            {
                if (ViewState["_PageSize"] == null || ViewState["_PageSize"].ToString().Length <= 0)
                {
                    ViewState["_PageSize"] = "20";
                }

                return int.Parse(ViewState["_PageSize"].ToString());
            }
            set
            {
                ViewState["_PageSize"] = value.ToString();
            }
        }

        public int PageCount
        {
            get
            {
                if (ViewState["_PageCount"] == null || ViewState["_PageCount"].ToString().Length <= 0)
                {
                    ViewState["_PageCount"] = "0";
                }

                return int.Parse(ViewState["_PageCount"].ToString());
            }
        }

        public int PageIndex
        {
            get
            {
                if (ViewState["_PageIndex"] == null || ViewState["_PageIndex"].ToString().Length <= 0)
                {
                    ViewState["_PageIndex"] = "0";
                }

                return int.Parse(ViewState["_PageIndex"].ToString());
            }
            set
            {
                ViewState["_PageIndex"] = value.ToString();
            }
        }

        public int RowCount
        {
            get
            {
                if (ViewState["_RowCount"] == null || ViewState["_RowCount"].ToString().Length <= 0)
                {
                    ViewState["_RowCount"] = "0";
                }

                return int.Parse(ViewState["_RowCount"].ToString());
            }
            set
            {
                ViewState["_RowCount"] = value.ToString();

                ViewState["_PageCount"] = (RowCount + PageSize - 1) / PageSize;
            }
        }



        protected void OnCommand(CommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "First": PageIndex = 0; break;
                case "Last": PageIndex = PageIndex - 1; break;
                case "Next": PageIndex = PageIndex + 1; break;
                case "Finally": PageIndex = PageCount - 1; break;
            }

            if (CommandEvent != null) CommandEvent(this, e);
        }

        Button[] ButtonList = new Button[] { new Button(), new Button(), new Button(), new Button() };

        string[] BLCommandID = new string[] { "First", "Last", "Next", "Finally" };

        string[] BLCommandName = new string[] { "首页", "上一页", "下一页", "尾页" };

        protected override void CreateChildControls()
        {
            for (int i = 0; i < ButtonList.Length; i++)
            {
                ButtonList[i].ID = BLCommandID[i];

                ButtonList[i].CommandName = BLCommandID[i];

                ButtonList[i].Text = BLCommandName[i];

                ButtonList[i].Command += delegate(object sender, System.Web.UI.WebControls.CommandEventArgs e)
                {
                    OnCommand(new CommandEventArgs(((Button)sender).ID, e));
                };

                this.Controls.Add(ButtonList[i]);
            }
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            output.Write("总记录数:");

            output.Write("&nbsp;");

            output.Write(RowCount.ToString());

            output.Write("&nbsp;");

            output.Write("每页:");

            output.Write("&nbsp;");

            output.Write(PageSize.ToString());

            output.Write("&nbsp;");

            output.Write("条");

            output.Write("&nbsp;");

            output.Write("当前页/总页数");

            output.Write("&nbsp;");

            output.Write(Convert.ToString(PageIndex + 1));

            output.Write("&nbsp;");

            output.Write("/");

            output.Write("&nbsp;");

            output.Write(PageCount.ToString());

            output.Write("&nbsp;");

            if (PageIndex <= 0)
            {
                ButtonList[0].Enabled = false;
                ButtonList[1].Enabled = false;
            }
            else
            {
                ButtonList[0].Enabled = true;
                ButtonList[1].Enabled = true;
            }

            if (PageIndex + 1 >= PageCount)
            {
                ButtonList[2].Enabled = false;
                ButtonList[3].Enabled = false;
            }
            else
            {
                ButtonList[2].Enabled = true;
                ButtonList[3].Enabled = true;
            }

            for (int i = 0; i < ButtonList.Length; i++)
            {
                ButtonList[i].RenderControl(output);

                output.Write("&nbsp;");
            }
        }
    }
}