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

推荐订阅源

P
Privacy & Cybersecurity Law Blog
SecWiki News
SecWiki News
T
Troy Hunt's Blog
Y
Y Combinator Blog
V
V2EX
美团技术团队
Last Week in AI
Last Week in AI
S
Security @ Cisco Blogs
IT之家
IT之家
博客园_首页
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
阮一峰的网络日志
阮一峰的网络日志
AI
AI
罗磊的独立博客
人人都是产品经理
人人都是产品经理
H
Hacker News: Front Page
N
News and Events Feed by Topic
P
Privacy International News Feed
V2EX - 技术
V2EX - 技术
Recent Commits to openclaw:main
Recent Commits to openclaw:main
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
GbyAI
GbyAI
L
LINUX DO - 热门话题
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Azure Blog
Microsoft Azure Blog
Martin Fowler
Martin Fowler
月光博客
月光博客
WordPress大学
WordPress大学
Latest news
Latest news
Google DeepMind News
Google DeepMind News
S
Schneier on Security
N
Netflix TechBlog - Medium
腾讯CDC
T
Tailwind CSS Blog
TaoSecurity Blog
TaoSecurity Blog
S
Secure Thoughts
L
LINUX DO - 最新话题
Project Zero
Project Zero
Cyberwarzone
Cyberwarzone
D
DataBreaches.Net
Webroot Blog
Webroot Blog
B
Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
S
SegmentFault 最新的问题
The GitHub Blog
The GitHub Blog
H
Help Net Security
L
LangChain Blog
A
Arctic Wolf
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻

博客园 - 阿福

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 Efficient Data Paging and Sorting with ASP.NET 2.0 and SQL 2005 JavaScript Calendar Cool MSDN ASP.NET 2.0 GridView Control Article Do I have to cry for you Google无法访问 扯淡 狗*! Atlas Control Toolkit and Source Code for the Build-in Asp.Net 2.0 Providers Great New Advanced Article on ASP.NET 2.0 Master Pages ASP.NET 2.0:通过SqlDataSource绑定数据到普通控件 通过客户端扩展实现固定GridView表头功能 不使用ISAPI或IIS wildcard实现不带扩展名URL的转向 VS 2005 Web Application Project & Atlas Control Toolkit & CSS Control Adapter ASP.NET 2.0 Language Swithcer and Theme Swicher 多语言转换和多样式主题转换 How to use virtual path providers to dynamically load and compile content from virtual paths in 
ASP.NET 2.0: Add build-in paging feature to repeater/为repeater添加内置分页功能
阿福 · 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]">