ASP. net2.0: addbuild-inpagingfeaturetorepeater/Add inner for Repeater

Source: Internet
Author: User

Keywords: Paging with Repeater control, dataource control, sqldatasource, Repeater control pagination

Introduction

Using repeater to list your data has 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 cocould 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]">     

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.