自定义分页控件

今天有时间,做了一个简单的自定义分页控件!------
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;
                }
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值