日期:2014-05-16  浏览次数:20862 次

今天完成的自定义分页控件,支持AJAX.DLL的方式支持24种分页样式

分页控件,大家应该都比较熟悉了。但是一直以来分页控件都给我一个很郁闷的地方,几乎都是URL传值,让人很郁闷。

如果我的是高级查询,里面的值有很多,那写死个人。同时对系统的请求也是一个考验。于是我写了这个分页控件。同时支持24种分页样式。

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.UI.HtmlControls;

namespace MyControl
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:DataPager runat=server></{0}:DataPager>")]
    public class DataPager : WebControl, INamingContainer, IPostBackEventHandler
    {
        public event EventHandler PagerOnclick
        {
            add
            {
                Events.AddHandler(this, value);
            }
            remove
            {
                Events.RemoveHandler(this, value);
            }
        }

        protected void _PagerClick(object sender, EventArgs e)
        {
            EventHandler hd = (EventHandler)base.Events[this];
            if (hd != null)
            {
                hd(sender, e);
            }
        }

        public void RaisePostBackEvent(string Index)
        {
            //Label lbl = (Label)this.FindControl("lbl");
            //lbl.Text += "自己的事件:" + Index;

        }
        void btn_Click(object sender, EventArgs e)
        {
            try
            {
                int pageIndex = int.Parse(((sender) as LinkButton).Text);
                this.PageIndex = pageIndex;
                _PagerClick(sender, e);
            }
            catch (Exception) { }

        }

        /// <summary>
        /// 当前页数
        /// </summary>
        public int PageIndex
        {
            get { return int.Parse(ViewState["PageIndex"] == null ? "1" : ViewState["PageIndex"].ToString()); }
            set
            {
                ViewState["PageIndex"] = value;
                CreateChildControls();
            }
        }

        /// <summary>
        /// 总的页数
        /// </summary>
        public int TotalPage
        {
            get { return int.Parse(ViewState["TotalPage"] == null ? "1" : ViewState["TotalPage"].ToString()); }
            set { ViewState["TotalPage"] = value; }
        }

        /// <summary>
        /// 总的记录数
        /// </summary>
        public int TotalRecord
        {
            get { return int.Parse(ViewState["TotalRecord"] == null ? "0" : ViewState["TotalRecord"].ToString()); }
            set { ViewState["TotalRecord"] = value; }
        }
        /// <summary>
        /// 每页数据大小
        /// </summary>
        public int PageSize
        {
            get { return int.Parse(ViewState["PageSize"] == null ? "0" : ViewState["PageSize"].ToString()); }
            set { ViewState["PageSize"] = value; }
        }
        /// <summary>
        /// 自定义说明
        /// </summary>
        public string CustomText
        {
            get { return ViewState["CustomText"] == null ? "" : ViewState["CustomText"].ToString(); }
            set { ViewState["CustomText"] = value; }
        }

        void NextPager(object send, EventArgs e)
        {
            this.PageIndex = this.PageIndex + 10 > this.TotalPage ? this.TotalPage : this.PageIndex + 10;
        }
        void PrePager(object send, EventArgs e)
        {
            this.PageIndex = this.PageIndex-10 > 1 ? this.PageIndex - 10 : 1;
        }
        void MaxPager(object send, EventArgs e)
        {
            this.PageIndex = this.TotalPage;
        }

        protected override void CreateChildControls()
        {
            this.Controls.Clear();
            HtmlGenericControl div = new HtmlGenericControl(&q