ASP.NET 2.0:Add build-in paging feature to repeater/repeaterに内蔵ページング機能を追加

11605 ワード

Keywords:paging with repeater control,dataource control,sqldatasource,repeater controlページング
Introduction
Using repeater to list your data has many benefits, it's light, hight-perfomance, and gives you more control to the layout. Especially when the data needs to be nested. It's difficult to control the item layout when using gridview. When used with datasource control, repeater can be codeless too, just like the gridview. But the drawback of the repeater is that it's lack of  build in paing and sorting feature when bind to datasource. That means you bind a repeater with a data sqlsource, this can be no code at all (I'm meaning the code in the codebehind file). But if you want to page the data, you have to throw away the sqldatasource and do the databind as the way you do in asp.net 1.x. Surely, this could make you very uncomfortalbe. In this article, I discuss how to add the paing feature to the repeater and does not lose the convenience of datasource databinding.
I gave out the source code first, you can download it here

public class Repeater : System.Web.UI.WebControls.Repeater

    {

        public Repeater()

        {

            this._pageCount = -1;

            this._recordCount = -1;

        }

        #region pager properties

        [DefaultValue(false)]

        public virtual bool AllowPaging

        {

            get

            {

                object obj2 = this.ViewState["AllowPaging"];

                if (obj2 != null)

                {

                    return (bool)obj2;

                }

                return false;

            }

            set

            {

                bool allowPaging = this.AllowPaging;

                if (value != allowPaging)

                {

                    this.ViewState["AllowPaging"] = value;

                    if (base.Initialized)

                    {

                        base.RequiresDataBinding = true;

                    }

                }

            }

        }

        private int _pageCount;

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]

        public virtual int PageCount

        {

            get

            {

                if (this._pageCount >= 0)

                {

                    return this._pageCount;

                }

                object obj2 = this.ViewState["PageCount"];

                if (obj2 == null)

                {

                    return 0;

                }

                return (int)obj2;

            }

        }

        private int _recordCount;

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]

        public virtual int RecordCount

        {

            get

            {

                if (this._recordCount >= 0)

                {

                    return this._recordCount;

                }

                object obj2 = this.ViewState["RecordCount"];

                if (obj2 == null)

                {

                    return 0;

                }

                return (int)obj2;

            }

        }

        private int _pageIndex;

        [Browsable(true), DefaultValue(0)]

        public virtual int PageIndex

        {

            get

            {

                return this._pageIndex;

            }

            set

            {

                if (value < 0)

                {

                    throw new ArgumentOutOfRangeException("value");

                }

                if (this.PageIndex != value)

                {

                    this._pageIndex = value;

                    if (base.Initialized)

                    {

                        base.RequiresDataBinding = true;

                    }

                }

            }

        }

        [DefaultValue(10)]

        public virtual int PageSize

        {

            get

            {

                object obj2 = this.ViewState["PageSize"];

                if (obj2 != null)

                {

                    return (int)obj2;

                }

                return 10;

            }

            set

            {

                if (value < 1)

                {

                    throw new ArgumentOutOfRangeException("value");

                }

                if (this.PageSize != value)

                {

                    this.ViewState["PageSize"] = value;

                    if (base.Initialized)

                    {

                        base.RequiresDataBinding = true;

                    }

                }

            }

        }

        #endregion

        #region additional templates

        private ITemplate _pagerTemplate;

        [Browsable(false), PersistenceMode(PersistenceMode.InnerProperty), DefaultValue((string)null), TemplateContainer(typeof(RepeaterItem))]

        public virtual ITemplate PagerTemplate

        {

            get

            {

                return this._pagerTemplate;

            }

            set

            {

                this._pagerTemplate = value;

            }

        }

        private ITemplate _emptyDataTemplate;

        [Browsable(false), DefaultValue((string)null), PersistenceMode(PersistenceMode.InnerProperty)]

        public virtual ITemplate EmptyDataTemplate

        {

            get

            {

                return this._emptyDataTemplate;

            }

            set

            {

                this._emptyDataTemplate = value;

            }

        }

 

        #endregion

        #region override methods

        protected override IEnumerable GetData()

        {

            IEnumerable data = base.GetData();

            if (data != null)

            {

                PagedDataSource source = new PagedDataSource();

                //source.AllowCustomPaging = false;

                source.DataSource = data;

                source.AllowPaging = this.AllowPaging;

                //source.AllowServerPaging = true ;

                source.CurrentPageIndex = this.PageIndex;

                //source.VirtualCount = 0;

                source.PageSize = this.PageSize;

                this.ViewState["PageCount"] = source.PageCount; ;

                this.ViewState["RecordCount"] = source.DataSourceCount;

                return source;

            }

            return data;

        }

        protected override void CreateControlHierarchy(bool useDataSource)

        {

            base.CreateControlHierarchy(useDataSource);

            if (this._pagerTemplate != null)

            {



                RepeaterItem pager = CreateItem(-1, ListItemType.Pager);

                this._pagerTemplate.InstantiateIn(pager);

                this.Controls.Add(pager);

                pager.DataBind();

            }

        }

        protected override void OnInit(EventArgs e)

        {

            base.OnInit(e);

            if (this.Page != null)

            {

                this.Page.RegisterRequiresControlState(this);

            }

        }

        protected override void LoadControlState(object savedState)

        {

            object[] objArray = savedState as object[];

            if (objArray != null)

            {

                base.LoadControlState(objArray[0]);

                if (objArray[1] != null)

                {

                    this._pageIndex = (int)objArray[1];

                }

            }

        }

        protected override object SaveControlState()

        {

            object obj2 = base.SaveControlState();

            if (obj2 != null || this._pageIndex != 0)

            {

                return new object[] { obj2, this._pageIndex };

            }

            return true;

        }

        protected override bool OnBubbleEvent(object sender, EventArgs e)

        {

            bool flag = false;

            

            RepeaterCommandEventArgs args = e as RepeaterCommandEventArgs;

            if (args != null)

            {

                

                if (args.CommandName.Equals(DataControlCommands.PageCommandName, StringComparison.OrdinalIgnoreCase))

                {

                    this.HandlePage(args);

                }

                else

                {

                    this.OnItemCommand(args);

                }

                flag = true;

            }

            return flag;

        }

        #endregion

        #region private methods

        private void HandlePage(RepeaterCommandEventArgs e)

        {

            bool causesValidation = false;

            string validationGroup = string.Empty;

            IButtonControl control = e.CommandSource as IButtonControl;

            if (control != null)

            {

                causesValidation = control.CausesValidation;

                validationGroup = control.ValidationGroup;

            }

            if (causesValidation)

            {

                this.Page.Validate(validationGroup);

            }

            string commondArgument = (string)e.CommandArgument;

            int newPageIndex = this.PageIndex;

            if (commondArgument.Equals(DataControlCommands.NextPageCommandArgument, StringComparison.OrdinalIgnoreCase))

            {

                newPageIndex++;

            }

            else if (commondArgument.Equals(DataControlCommands.PreviousPageCommandArgument, StringComparison.OrdinalIgnoreCase))

            {

                newPageIndex--;

            }

            else if (commondArgument.Equals(DataControlCommands.FirstPageCommandArgument, StringComparison.OrdinalIgnoreCase))

            {

                newPageIndex = 0;

            }

            else if (commondArgument.Equals(DataControlCommands.LastPageCommandArgument, StringComparison.OrdinalIgnoreCase))

            {

                if (base.IsViewStateEnabled)

                {

                    newPageIndex = this.PageCount - 1;

                }

                else

                {

                    newPageIndex = 0x7fffffff;

                }

            }

            else

            {

                newPageIndex = Convert.ToInt32(commondArgument, System.Globalization.CultureInfo.InvariantCulture) - 1;

            }

            if (this.AllowPaging && IsBoundUsingDataSourceID)

            {

                if (newPageIndex < 0) return;

                if (newPageIndex > this.PageCount - 1) return;

                this._pageIndex = newPageIndex;

                base.RequiresDataBinding = true;

            }

        }

        #endregion

    }

How to use:    

   

        AllowPaging="True">

        

            

            



        

        

            

            

            

            

        

    

    

        SelectCommand="SELECT [PurchaseOrderNo], [PurchaseOrderID] FROM [poPurchaseOrder]">