惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
博客园 - 聂微东
博客园 - Franky
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
雷峰网
雷峰网
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
L
LangChain Blog
WordPress大学
WordPress大学
H
Help Net Security
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Y
Y Combinator Blog
Blog — PlanetScale
Blog — PlanetScale
MyScale Blog
MyScale Blog
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
罗磊的独立博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
有赞技术团队
有赞技术团队
Apple Machine Learning Research
Apple Machine Learning Research
云风的 BLOG
云风的 BLOG
博客园 - 【当耐特】
P
Proofpoint News Feed
D
DataBreaches.Net

博客园 - 阿福

TypeScript forms authentication failed the ticket supplied was invalid错误 (Windows Server 2008 + IIS 7.5) jQuery Mobile 1.1.1 RC1发布 HashSet<T> vs List<T> 不要用把无序GUID既作为主键又作为聚集索引 WCF Data Services 5.0 RTM发布 EF Power Tools Beta 2发布 Entity Framework 5性能方面的注意事项 jQuery Mobile 1.1.0 RC2发布 源码+幻灯片:学习HTML5/jQuery/ASP.NET MVC/EF Code First的绝佳资源Account at a Glance项目 使用Autofac在ASP.NET Web API上实现依赖注入 在Windows Azure上开发ASP.NET程序与在Windows Sever上有何不同 充分利用缓存来提高网站性能 ASP.NET MVC 4, ASP.NET Web API, ASP.NET Web Pages v2 (Razor)全部开源,并接受来自社区的贡献(contributions) EF5 beta2通过NuGet发布 Getting Started with HTML5 开发HTML5应用你需要了解的 WCF入门资源 jquery history plugin, url hash Run Tasks in an ASP.NET Application Domain ASP.NET 2.0: 生成Excel报表 VS 2008 hot-fix终于出来了 Images; How to create an HTTP handler to dynamically resize images and change quality. Tip/Trick ASP.NET 2.0: DropDownList DataBind ASP.NET 2.0: Forms Authentication Across domains Ajax类库、框架、工具包完全列表 怀旧啊 Search Box ASP.NET 2.0: Site Maps Checking All CheckBoxes in a GridView
ASP.NET 2.0: Add build-in&...
阿福 · 2010-01-01 · via 博客园 - 阿福

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]">