最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Repeater和GridView支持DataPager分页做法
时间:2012-02-24 编辑:简简单单 来源:一聚教程网
改造办法是自己写一个控件,让它继承GridView或Repeater,并实现IPageableItemContainer 接口。下面要发的是国外某高手写的代码,测试有效。具体使用的时候,要建一个类库项目,把代码编译成dll后,就可以添加到VS的工具箱里了!
一、自定义Repeater
代码如下 | 复制代码 |
using System.Web.UI; namespace WYJ.Web.Controls /// /// /// /// //if (Page.IsPostBack) // if (!IsBoundUsingDataSourceID && PersistentDataSource && ViewState["DataSource"] != null) protected override void OnLoad(System.EventArgs e) if (!IsBoundUsingDataSourceID && PersistentDataSource && ViewState["DataSource"] != null) } base.OnLoad(e); }
if (TotalRows > -1) /// /// if (dataObjects == null && this.DataSource != null) if (!PagingInDataSource && MaximumRows != -1 && dataObjects != null) if (dataObjects != null) } ViewState["TotalRows"] = i; if (!IsBoundUsingDataSourceID && PersistentDataSource) SetPageProperties(StartRowIndex, MaximumRows, true); if (PagingInDataSource && !Page.IsPostBack) return dataObjects;
/// /// } }
|
ASPX页面要做的事情(以我网站的留言板为例):
首先得把标签注册进来
代码如下 | 复制代码 |
<%@ Register Assembly="WYJ.Web.Controls" Namespace="WYJ.Web.Controls" TagPrefix="WYJ" %> |
然后添加我们的Repeater
之后添加.NET自带的DataPager,并自定义一些分页样式
代码如下 | 复制代码 |
共<%=Math.Ceiling((double)DataPager1.TotalRowCount / DataPager1.PageSize)%>页,<%=DataPager1.TotalRowCount%>条记录,每页显示 <asp:LinkButton ID="lnkbtn10" CssClass="currentpagesize" runat="server" OnClick="lnkbtn10_Click">10
|
后台代码:
分页部分不需要代码。下面发的代码是切换每页显示数量的:
代码如下 | 复制代码 |
protected void lnkbtn10_Click(object sender, EventArgs e) protected void lnkbtn20_Click(object sender, EventArgs e) protected void lnkbtn30_Click(object sender, EventArgs e)
|
二、自定义GridView
代码如下 | 复制代码 |
using System; namespace WYJ.Web.Controls /// /// // if the paging feature is enabled, determine the total number of rows in the datasource // raise the row count available event // make sure the top and bottom pager rows are not visible if (this.BottomPagerRow != null) /// if (this.PageIndex != newPageIndex) isCanceled = args.Cancel; // if the event wasn't cancelled change the paging values if (databind) /// /// /// /// |
用法与Repeater类似,不多发了~
相关文章
- C#复制数组的两种方式及效率比较解读 10-24
- ASP.NET Identity用法解析 10-24
- ASP.NET MVC使用Identity增删改查用户介绍 10-24
- C语言中atoi函数模拟实现介绍 10-18
- .Net反向代理组件Yarp用法介绍 10-10
- .NET使用YARP通过编码方式配置域名转发实现反向代理教程 10-10