摘要:总所周知Repeater,本身是不能分页的。不过Repeater有这样一个方法
protected virtual IEnumerable GetData(),因此我们可以override这个方法,并添加一些分页的想关属性
代码如下:
Code
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.ComponentModel;
using System.Drawing.Design;
using System.Web.UI.Design;
using System.Text;
using System.Collections;
namespace Sagacity.Controls
{
public class PageReapter : Repeater
{
private static readonly object EventPageIndexChanged = new object();
PagedDataSource pagedDataSource;
[DefaultValue(true), Category("Paging")]
public virtual bool AllowPaging
{
get
{
object obj2 = this.ViewState["AllowPaging"];
if (obj2 != null)
{
return (bool)obj2;
}
return true;
}
set
{
this.ViewState["AllowPaging"] = value;
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(false)]
public virtual int VirtualItemCount
{
get
{
object obj2 = this.ViewState["VirtualItemCount"];
if (obj2 != null)
{
return (int)obj2;
}
return 0;
}
set
{
if (value < 0)
throw new ArgumentOutOfRangeException("value");
this.ViewState["VirtualItemCount"] = value;
}
}
[DefaultValue(10), Browsable(true), Category("Paging")]
public virtual int PageSize
{
get
{
object obj2 = this.ViewState["PageSize"];
if (obj2 != null)
{
return (int)obj2;
}
return 10;
}
set
{
if (value < 0)
throw new ArgumentOutOfRangeException("value");
this.ViewState["PageSize"] = value;
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(false)]
public virtual int PageCount
{
get
{
object obj2 = this.ViewState["PageCount"];
if (obj2 != null)
{
return (int)obj2;
}
return 0;
}
set
{
if (value < 0)
throw new ArgumentOutOfRangeException("value");
this.ViewState["PageCount"] = value;
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(false)]
public virtual int CurrentPageIndex
{
get
{
object obj2 = this.ViewState["CurrentPageIndex"];
if (obj2 != null)
{
return (int)obj2;
}
return 0;
}
set
{
if (value < 0)
throw new ArgumentOutOfRangeException("value");
this.ViewState["CurrentPageIndex"] = value;
OnPageIndexChanged(new DataGridPageChangedEventArgs(this, value));
}
}
protected virtual void OnPageIndexChanged(DataGridPageChangedEventArgs e)
{
DataGridPageChangedEventHandler handler = (DataGridPageChangedEventHandler)Events[EventPageIndexChanged];
if (handler != null)
{
handler(this, e);
}
}
[Category("Action")]
public event DataGridPageChangedEventHandler PageIndexChanged
{
add
{
base.Events.AddHandler(EventPageIndexChanged, value);
}
remove
{
base.Events.RemoveHandler(EventPageIndexChanged, value);
}
}
protected override System.Collections.IEnumerable GetData()
{
IEnumerable dataSource = base.GetData();
if (AllowPaging && dataSource != null)
{
pagedDataSource = new PagedDataSource();
ICollection collection = dataSource as ICollection;
if (collection != null)
{
pagedDataSource.VirtualCount = collection.Count;
VirtualItemCount = pagedDataSource.VirtualCount;
}
else if (dataSource is IListSource)
{
pagedDataSource.VirtualCount = (dataSource as IListSource).GetList().Count;
VirtualItemCount = pagedDataSource.VirtualCount;
}
else if (VirtualItemCount > 0)
{
pagedDataSource.VirtualCount = VirtualItemCount;
}
else
{
pagedDataSource = null;
return dataSource;
}
pagedDataSource.DataSource = dataSource;
pagedDataSource.CurrentPageIndex = CurrentPageIndex;
pagedDataSource.PageSize = PageSize;
pagedDataSource.AllowPaging = true;
pagedDataSource.AllowCustomPaging = false;
ViewState["PageCount"] = pagedDataSource.PageCount;
return pagedDataSource;
}
return dataSource;
}
}
}
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.ComponentModel;
using System.Drawing.Design;
using System.Web.UI.Design;
using System.Text;
using System.Collections;
namespace Sagacity.Controls
{
public class PageReapter : Repeater
{
private static readonly object EventPageIndexChanged = new object();
PagedDataSource pagedDataSource;
[DefaultValue(true), Category("Paging")]
public virtual bool AllowPaging
{
get
{
object obj2 = this.ViewState["AllowPaging"];
if (obj2 != null)
{
return (bool)obj2;
}
return true;
}
set
{
this.ViewState["AllowPaging"] = value;
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(false)]
public virtual int VirtualItemCount
{
get
{
object obj2 = this.ViewState["VirtualItemCount"];
if (obj2 != null)
{
return (int)obj2;
}
return 0;
}
set
{
if (value < 0)
throw new ArgumentOutOfRangeException("value");
this.ViewState["VirtualItemCount"] = value;
}
}
[DefaultValue(10), Browsable(true), Category("Paging")]
public virtual int PageSize
{
get
{
object obj2 = this.ViewState["PageSize"];
if (obj2 != null)
{
return (int)obj2;
}
return 10;
}
set
{
if (value < 0)
throw new ArgumentOutOfRangeException("value");
this.ViewState["PageSize"] = value;
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(false)]
public virtual int PageCount
{
get
{
object obj2 = this.ViewState["PageCount"];
if (obj2 != null)
{
return (int)obj2;
}
return 0;
}
set
{
if (value < 0)
throw new ArgumentOutOfRangeException("value");
this.ViewState["PageCount"] = value;
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(false)]
public virtual int CurrentPageIndex
{
get
{
object obj2 = this.ViewState["CurrentPageIndex"];
if (obj2 != null)
{
return (int)obj2;
}
return 0;
}
set
{
if (value < 0)
throw new ArgumentOutOfRangeException("value");
this.ViewState["CurrentPageIndex"] = value;
OnPageIndexChanged(new DataGridPageChangedEventArgs(this, value));
}
}
protected virtual void OnPageIndexChanged(DataGridPageChangedEventArgs e)
{
DataGridPageChangedEventHandler handler = (DataGridPageChangedEventHandler)Events[EventPageIndexChanged];
if (handler != null)
{
handler(this, e);
}
}
[Category("Action")]
public event DataGridPageChangedEventHandler PageIndexChanged
{
add
{
base.Events.AddHandler(EventPageIndexChanged, value);
}
remove
{
base.Events.RemoveHandler(EventPageIndexChanged, value);
}
}
protected override System.Collections.IEnumerable GetData()
{
IEnumerable dataSource = base.GetData();
if (AllowPaging && dataSource != null)
{
pagedDataSource = new PagedDataSource();
ICollection collection = dataSource as ICollection;
if (collection != null)
{
pagedDataSource.VirtualCount = collection.Count;
VirtualItemCount = pagedDataSource.VirtualCount;
}
else if (dataSource is IListSource)
{
pagedDataSource.VirtualCount = (dataSource as IListSource).GetList().Count;
VirtualItemCount = pagedDataSource.VirtualCount;
}
else if (VirtualItemCount > 0)
{
pagedDataSource.VirtualCount = VirtualItemCount;
}
else
{
pagedDataSource = null;
return dataSource;
}
pagedDataSource.DataSource = dataSource;
pagedDataSource.CurrentPageIndex = CurrentPageIndex;
pagedDataSource.PageSize = PageSize;
pagedDataSource.AllowPaging = true;
pagedDataSource.AllowCustomPaging = false;
ViewState["PageCount"] = pagedDataSource.PageCount;
return pagedDataSource;
}
return dataSource;
}
}
}
前台页面cs代码
Code
void btnLast_Click(object sender, EventArgs e)
{
BindPage(rTeamList.PageCount - 1);
}
void btnNext_Click(object sender, EventArgs e)
{
BindPage(rTeamList.CurrentPageIndex + 1);
}
void btnPrev_Click(object sender, EventArgs e)
{
BindPage(rTeamList.CurrentPageIndex - 1);
}
void btnFirst_Click(object sender, EventArgs e)
{
BindPage(0);
}
void rTeamList_PreRender(object sender, EventArgs e)
{
btnFirst.Enabled = btnPrev.Enabled = (rTeamList.CurrentPageIndex != 0);
btnLast.Enabled = btnNext.Enabled = (rTeamList.CurrentPageIndex < rTeamList.PageCount - 1);
lblCurrentPage.Text = (rTeamList.CurrentPageIndex + 1).ToString();
lblTotalPage.Text = rTeamList.PageCount.ToString();
}
void btnLast_Click(object sender, EventArgs e)
{
BindPage(rTeamList.PageCount - 1);
}
void btnNext_Click(object sender, EventArgs e)
{
BindPage(rTeamList.CurrentPageIndex + 1);
}
void btnPrev_Click(object sender, EventArgs e)
{
BindPage(rTeamList.CurrentPageIndex - 1);
}
void btnFirst_Click(object sender, EventArgs e)
{
BindPage(0);
}
void rTeamList_PreRender(object sender, EventArgs e)
{
btnFirst.Enabled = btnPrev.Enabled = (rTeamList.CurrentPageIndex != 0);
btnLast.Enabled = btnNext.Enabled = (rTeamList.CurrentPageIndex < rTeamList.PageCount - 1);
lblCurrentPage.Text = (rTeamList.CurrentPageIndex + 1).ToString();
lblTotalPage.Text = rTeamList.PageCount.ToString();
}