asp.net中让Repeater和GridView支持DataPager分页
网络编程 2021-07-04 22:40www.168986.cn编程入门
.NET 3.5中的DataPager碉堡了,可惜只支持ListView。传统的GridView和Repeater都无法直接使用DataPager分页。但我们如果稍加改造,就可以让Repeater和GridView支持DataPager分页
改造办法是自己写一个控件,让它继承GridView或Repeater,并实现IPageableItemContainer 接口。狼蚁网站SEO优化要发的是国外某高手写的代码,测试有效。具体使用的时候,要建一个类库项目,把代码编译成dll后,就可以添加到VS的工具箱里了!
一、自定义Repeater
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WYJ.Web.Controls
{
/// <summary>
/// Repeater with support for DataPager
/// </summary>
[ToolboxData("<{0}:DataPagerRepeater runat=server PersistentDataSource=true></{0}:DataPagerRepeater>")]
public class DataPagerRepeater : Repeater, System.Web.UI.WebControls.IPageableItemContainer, INamingContainer
{
/// <summary>
/// Number of rows to show
/// </summary>
public int MaximumRows { get { return ViewState["MaximumRows"] != null ? (int)ViewState["MaximumRows"] : -1; } }
/// <summary>
/// First row to show
/// </summary>
public int StartRowIndex { get { return ViewState["StartRowIndex"] != null ? (int)ViewState["StartRowIndex"] : -1; } }
/// <summary>
/// Total rows. When PagingInDataSource is set to true you must get the total records from the datasource (without paging) at the FetchingData event
/// When PagingInDataSource is set to true you also need to set this when you load the data the first time.
/// </summary>
public int TotalRows { get { return ViewState["TotalRows"] != null ? (int)ViewState["TotalRows"] : -1; } set { ViewState["TotalRows"] = value; } }
/// <summary>
/// If repeater should store data source in view state. If false you need to get and bind data at post back. When using a connected data source this is handled by the data source.
/// </summary>
public bool PersistentDataSource
{
get { return ViewState["PersistentDataSource"] != null ? (bool)ViewState["PersistentDataSource"] : true; }
set { ViewState["PersistentDataSource"] = value; }
}
/// <summary>
/// Set to true if you want to handle paging in the data source.
/// Ex if you are selecting data from the database and only select the current rows
/// you must set this property to true and get and rebind data at the FetchingData event.
/// If this is true you must also set the TotalRecords property at the FetchingData event.
/// </summary>
/// <seealso cref="FetchingData"/>
/// <seealso cref="TotalRows"/>
public bool PagingInDataSource
{
get { return ViewState["PageingInDataSource"] != null ? (bool)ViewState["PageingInDataSource"] : false; }
set { ViewState["PageingInDataSource"] = value; }
}
/// <summary>
/// Checks if you need to rebind data source at postback
/// </summary>
public bool NeedsDataSource
{
get
{
if (PagingInDataSource)
return true;
if (IsBoundUsingDataSourceID == false && !Page.IsPostBack)
return true;
if (IsBoundUsingDataSourceID == false && PersistentDataSource == false && Page.IsPostBack)
return true;
else
return false;
}
}
/// <summary>
/// Loading ViewState
/// </summary>
/// <param name="savedState"></param>
protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
//if (Page.IsPostBack)
//{
// if (!IsBoundUsingDataSourceID && PersistentDataSource && ViewState["DataSource"] != null)
// {
// this.DataSource = ViewState["DataSource"];
// this.DataBind(true);
// }
// if (IsBoundUsingDataSourceID)
// {
// this.DataBind();
// }
/
一、自定义Repeater
代码如下:
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WYJ.Web.Controls
{
/// <summary>
/// Repeater with support for DataPager
/// </summary>
[ToolboxData("<{0}:DataPagerRepeater runat=server PersistentDataSource=true></{0}:DataPagerRepeater>")]
public class DataPagerRepeater : Repeater, System.Web.UI.WebControls.IPageableItemContainer, INamingContainer
{
/// <summary>
/// Number of rows to show
/// </summary>
public int MaximumRows { get { return ViewState["MaximumRows"] != null ? (int)ViewState["MaximumRows"] : -1; } }
/// <summary>
/// First row to show
/// </summary>
public int StartRowIndex { get { return ViewState["StartRowIndex"] != null ? (int)ViewState["StartRowIndex"] : -1; } }
/// <summary>
/// Total rows. When PagingInDataSource is set to true you must get the total records from the datasource (without paging) at the FetchingData event
/// When PagingInDataSource is set to true you also need to set this when you load the data the first time.
/// </summary>
public int TotalRows { get { return ViewState["TotalRows"] != null ? (int)ViewState["TotalRows"] : -1; } set { ViewState["TotalRows"] = value; } }
/// <summary>
/// If repeater should store data source in view state. If false you need to get and bind data at post back. When using a connected data source this is handled by the data source.
/// </summary>
public bool PersistentDataSource
{
get { return ViewState["PersistentDataSource"] != null ? (bool)ViewState["PersistentDataSource"] : true; }
set { ViewState["PersistentDataSource"] = value; }
}
/// <summary>
/// Set to true if you want to handle paging in the data source.
/// Ex if you are selecting data from the database and only select the current rows
/// you must set this property to true and get and rebind data at the FetchingData event.
/// If this is true you must also set the TotalRecords property at the FetchingData event.
/// </summary>
/// <seealso cref="FetchingData"/>
/// <seealso cref="TotalRows"/>
public bool PagingInDataSource
{
get { return ViewState["PageingInDataSource"] != null ? (bool)ViewState["PageingInDataSource"] : false; }
set { ViewState["PageingInDataSource"] = value; }
}
/// <summary>
/// Checks if you need to rebind data source at postback
/// </summary>
public bool NeedsDataSource
{
get
{
if (PagingInDataSource)
return true;
if (IsBoundUsingDataSourceID == false && !Page.IsPostBack)
return true;
if (IsBoundUsingDataSourceID == false && PersistentDataSource == false && Page.IsPostBack)
return true;
else
return false;
}
}
/// <summary>
/// Loading ViewState
/// </summary>
/// <param name="savedState"></param>
protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
//if (Page.IsPostBack)
//{
// if (!IsBoundUsingDataSourceID && PersistentDataSource && ViewState["DataSource"] != null)
// {
// this.DataSource = ViewState["DataSource"];
// this.DataBind(true);
// }
// if (IsBoundUsingDataSourceID)
// {
// this.DataBind();
// }
/
编程语言
- 宿迁百度关键词排名指南:实现精准营销的关键
- 四川SEO优化怎么做网络推广
- 立昂技术备案老域名收购:如何为您的业务赋能
- 安徽百度关键词seo贵不贵,一般需要多少钱
- 吉林百度快照排名怎么做电话营销
- 多伦新手做SEO怎么做
- 甘肃优化关键词排名推广怎么做论坛营销
- 沙雅SEO网站推广:提升您的在线可见性
- 四川SEO优化如何提升销售额和销售量
- 聂荣网站排名优化:提升网站可见性的全方位指
- 涞水SEO:提升地方企业在线可见性的策略
- 辽宁百度seo排名怎样做网站排名
- 临湘哪有关键词排名优化:提升网站可见度的关
- 黑龙江百度网站优化有没有优惠
- 凉城优化关键词排名推广:提升您的网络可见性
- 萝北整站优化:提升您网站流量和排名的全面指