自定义分页控件

来源:互联网 发布:威廉马歇尔 知乎 编辑:程序博客网 时间:2024/05/23 12:30
今天有时间,做了一个简单的自定义分页控件!------
using System;using System.Collections.Generic;using System.ComponentModel;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Web.UI;namespace TextBoxControl{    /// <summary>    /// PageList 的摘要说明。    /// </summary>    [DefaultProperty("PageList"),     ToolboxData("<{0}:MyPageList runat=server></{0}:MyPageList>")]    public class PageList : System.Web.UI.WebControls.WebControl, IPostBackEventHandler    {        public PageList()        {            this.PageIndex = 0;            this.ToCount = 100;            this.PageSize = 10;        }        /// <summary>        /// 当前页索引        /// </summary>        [Bindable(true), Category("pageInfo"), DefaultValue(0)]           public int PageIndex        {            get            {                object o = ViewState["PageIndex"];                if (o == null)                {                    return 0;                }                else                {                    return (int)o;                }            }            set            {                ViewState["PageIndex"] = value;               }        }        /// <summary>        /// 总页数        /// </summary>        [Bindable(true)]        [Category("Data")]        [DefaultValue(0)]        private int PageCount        {            get            {                return int.Parse(Math.Ceiling(1.0 * ToCount / PageSize).ToString());             }        }        /// <summary>        /// 每页显示条数        /// </summary>        [Bindable(true), Category("pageInfo"), DefaultValue(0)]        public int PageSize        {            get            {                object o = ViewState["PageSize"];                if (o == null)                {                    return 0;                }                else                {                    return (int)o;                }            }            set            {                ViewState["PageSize"] = value;            }        }        /// <summary>        /// 数据总条数        /// </summary>        [Bindable(true), Category("pageInfo"), DefaultValue(0)]        public int ToCount        {            get            {                object o = ViewState["ToCount"];                if (o == null)                {                    return 0;                }                else                {                    return (int)o;                }            }            set            {                ViewState["ToCount"] = value;            }        }        /// <summary>        /// 输出HTML        /// </summary>        /// <param name="output"></param>        protected override void Render(HtmlTextWriter output)        {            output.Write("共:" + this.PageCount.ToString() + "页  ");            output.Write("<a href=\"javascript:" + Page.GetPostBackEventReference(this, "Home") + "\">首页</a>  ");            output.Write("<a href=\"javascript:" + Page.GetPostBackEventReference(this, "PV") + "\">上一页</a>  ");            output.Write(getPageList());            output.Write("<a href=\"javascript:" + Page.GetPostBackEventReference(this, "NEX") + "\">下一页</a>  ");            output.Write("<a href=\"javascript:" + Page.GetPostBackEventReference(this, "WEI") + "\">尾页</a>");        }        /// <summary>        /// 页码处理        /// </summary>        /// <returns></returns>        public string getPageList()        {            StringBuilder sb = new StringBuilder();            for (int i = 0; i < this.PageCount; i++)            {                if (i == this.PageIndex)                {                    sb.Append((i + 1).ToString() + "  ");                }                else                {                    sb.Append("<a href=\"javascript:" + Page.GetPostBackEventReference(this, i.ToString()) + "\">" + (i + 1).ToString() + "</a>  ");                }            }            return sb.ToString();        }        /// <summary>        /// 回发事件        /// </summary>        /// <param name="eventArgument"></param>        public void RaisePostBackEvent(string eventArgument)        {            if (eventArgument == "Home")            {                this.PageIndex = 0;            }            else if (eventArgument == "PV")            {                this.PageIndex=this.PageIndex == 0 ? 0 : this.PageIndex - 1;            }            else if (eventArgument == "NEX")            {                this.PageIndex = this.PageIndex == this.PageCount - 1 ? this.PageCount - 1 : this.PageIndex + 1;            }            else if (eventArgument == "WEI")            {                this.PageIndex = this.PageCount-1;            }            else            {                try                {                    this.PageIndex = int.Parse(eventArgument.ToString());                }                catch                {                    this.PageIndex = 0;                }            }        }    }}

原创粉丝点击