asp.net 分页控件

好久不用webform了,这次做的项目又要使用webform(微软推荐使用的是mvc),闲着没事儿干了的时候写个分页控件出来,废话不多说直接上代码

[

        DefaultEvent("Paging"),

        DefaultProperty("Text"),

        AspNetHostingPermission(SecurityAction.Demand,

            Level = AspNetHostingPermissionLevel.Minimal),

        AspNetHostingPermission(SecurityAction.InheritanceDemand,

            Level = AspNetHostingPermissionLevel.Minimal),

        ToolboxData("<{0}:Pager runat=\"server\"></{0}:Pager>")

    ]

    public class Pager : CompositeControl

    {

        private LinkButton _lbtnPrevious;

        private LinkButton _lbtnNext;

        private DropDownList _ddlPageItem;



        private static readonly object EventPagingKey =

            new object();



        [Bindable(true)]

        [Category("Appearance")]

        [DefaultValue("")]

        [Localizable(true)]

        public string Text

        {

            get

            {

                string s = (string) ViewState["Text"];

                return (s ?? string.Empty);

            }



            set { ViewState["Text"] = value; }

        }



        [Category("Appearance")]

        [DefaultValue(10)]

        [Localizable(true)]

        public int PageSize

        {

            get

            {

                string s = (string) ViewState["PageSize"];

                int pageSize = 0;

                int.TryParse(s, out pageSize);

                return pageSize;

            }

            set { ViewState["PageSize"] = value.ToString(); }

        }



        [Category("Appearance")]

        [DefaultValue(1)]

        [Localizable(true)]

        public int CurrentPageIndex

        {

            get

            {

                string s = (string) ViewState["CurrentPageIndex"];

                int currentPageIndex = 0;

                int.TryParse(s, out currentPageIndex);

                if (currentPageIndex < 1)

                    currentPageIndex = 1;

                return currentPageIndex;

            }

            set { ViewState["CurrentPageIndex"] = value.ToString(); }

        }



        [Category("Appearance")]

        [DefaultValue(0)]

        [Localizable(true)]

        public int RecordCount

        {

            get

            {

                string s = (string) ViewState["RecordCount"];

                int recordCount = 0;

                int.TryParse(s, out recordCount);

                return recordCount;

            }

            set { ViewState["RecordCount"] = value.ToString(); }

        }



        [Category("Appearance")]

        [DefaultValue(0)]

        [Localizable(true)]

        public int PageCount

        {

            get

            {

                string s = (string) ViewState["PageCount"];

                int pageCount = 0;

                int.TryParse(s, out pageCount);

                return pageCount;

            }

            private set { ViewState["PageCount"] = value.ToString(); }

        }



        private void BuildChildControls()

        {

            if (PageSize > 0)

            {

                PageCount = RecordCount / PageSize;

                if (RecordCount % PageSize != 0)

                    PageCount++;

            }



            _lbtnPrevious = new LinkButton { Text = "上一页", ID = "lbtnPrevious", Enabled = (CurrentPageIndex > 1) };

            _lbtnPrevious.Click += _lbtnPrevious_Click;

            this.Controls.Add(_lbtnPrevious);



            _ddlPageItem = new DropDownList { ID = "ddlPageItem", AutoPostBack = true };

            _ddlPageItem.SelectedIndexChanged += _ddlPageItem_SelectedIndexChanged;

            for (var i = 1; i <= PageCount; i++)

            {

                _ddlPageItem.Items.Add(new ListItem(string.Format("第{0}页", i), i.ToString()));

            }

            this.Controls.Add(_ddlPageItem);



            _lbtnNext = new LinkButton { Text = "下一页", ID = "lbtnNext", Enabled = (CurrentPageIndex < PageCount) };

            _lbtnNext.Click += _lbtnNext_Click;

            this.Controls.Add(_lbtnNext);

        }



        protected override void CreateChildControls()

        {

            this.Controls.Clear();

            ClearChildControlState();

            BuildChildControls();

        }



        private void _ddlPageItem_SelectedIndexChanged(object sender, EventArgs e)

        {

            int pageIndex;

            int.TryParse(_ddlPageItem.SelectedItem.Value, out pageIndex);

            CurrentPageIndex = pageIndex;

            OnPaging(e);

        }



        public event EventHandler Paging

        {

            add { this.Events.AddHandler(EventPagingKey, value); }

            remove { this.Events.RemoveHandler(EventPagingKey, value); }

        }



        protected virtual void OnPaging(EventArgs e)

        {

            var submitHandler = (EventHandler) Events[EventPagingKey];

            if (submitHandler != null)

            {

                submitHandler(this, e);

            }



            _lbtnPrevious.Enabled = CurrentPageIndex > 1;

            _lbtnNext.Enabled = CurrentPageIndex < PageCount;



            _ddlPageItem.SelectedValue = CurrentPageIndex.ToString();

        }



        private void _lbtnPrevious_Click(object sender, EventArgs e)

        {

            if (CurrentPageIndex > 1)

            {

                CurrentPageIndex -= 1;

                OnPaging(e);

            }

            else

            {

                _lbtnPrevious.Enabled = false;

            }

        }



        private void _lbtnNext_Click(object sender, EventArgs e)

        {

            if (CurrentPageIndex < PageCount)

            {

                CurrentPageIndex += 1;

                OnPaging(e);

            }

            else

            {

                _lbtnNext.Enabled = false;

            }

        }



        protected override void Render(HtmlTextWriter writer)

        {

            base.Render(writer);

        }

    }

功能比较简单比较适用于手机端asp.net开发,PC端不建议用下接框把页码都列出来,欢迎改进

你可能感兴趣的:(asp.net)