请教一个分页问题

luozhuang2009 2009-07-14 01:03:39
GridView较之DataGrid提供了更加强大、更加完善的功能,而且具备了丰富的可扩展功能,可以使用GridView提供的pagertemplate自定义分页模板:

事实上,GridView默认的几中分页样式,都是将相关按钮的CommandName设为Page,而CommandArgument设为相关参数,可接受的参数包括,first,last,prev,next,<PageIndex>(具体数字),然后按事件回溯,触发顶层的RowCommand,因此我们页可以使用这些默认的可识别的参数自定义自己的分页模板,asp.net会自动设置当前的NewPageIndex,而不需要任何的冗余代码。

.aspx页面:


<asp:gridview id="GridView1" runat="server" allowpaging="True" pagesize="10"
autogeneratecolumns="False" datasourceid="SqlDataSource1"
onpageindexchanging="GridView1_PageIndexChanging">
<columns>
<asp:boundfield datafield="CompanyName" headertext="CompanyName" sortexpression="CompanyName" />
<asp:boundfield datafield="ContactTitle" headertext="ContactTitle" sortexpression="ContactTitle" />
<asp:boundfield datafield="Phone" headertext="Phone" sortexpression="Phone" />
<asp:boundfield datafield="Fax" headertext="Fax" sortexpression="Fax" />
<asp:boundfield datafield="ContactName" headertext="ContactName" sortexpression="ContactName" />
</columns>
<pagertemplate>
<table width="100%">
<tr>
<td style="text-align:right">
第<asp:Label id="lblPageIndex" runat="server" text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>' />页
共/<asp:Label id="lblPageCount" runat="server" text='<%# ((GridView)Container.Parent.Parent).PageCount %>' />页
<asp:linkbutton id="btnFirst" runat="server" causesvalidation="False" commandargument="First" commandname="Page" text="首页" />
<asp:linkbutton id="btnPrev" runat="server" causesvalidation="False" commandargument="Prev" commandname="Page" text="上一页" />
<asp:linkbutton id="btnNext" runat="server" causesvalidation="False" commandargument="Next" commandname="Page" text="下一页" />
<asp:linkbutton id="btnLast" runat="server" causesvalidation="False" commandargument="Last" commandname="Page" text="尾页" />
<asp:textbox id="txtNewPageIndex" runat="server" width="20px" text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>' />
<asp:linkbutton id="btnGo" runat="server" causesvalidation="False" commandargument="-1" commandname="Page" text="GO" /><!-- here set the CommandArgument of the Go Button to '-1' as the flag -->
</td>
</tr>
</table>
</pagertemplate>
</asp:gridview>


<asp:sqldatasource id="SqlDataSource1" runat="server" connectionstring="Data Source=.;Initial Catalog=Northwind;Integrated Security=True"
providername="System.Data.SqlClient" selectcommand="SELECT [CompanyName], [ContactTitle], [Phone], [Fax], [ContactName] FROM [Customers]">
</asp:sqldatasource>
PageIndexChanging处理程序:
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView theGrid = sender as GridView; // refer to the GridView
int newPageIndex = 0;

if (-2 == e.NewPageIndex) { // when click the "GO" Button
TextBox txtNewPageIndex = null;
//GridViewRow pagerRow = theGrid.Controls[0].Controls[theGrid.Controls[0].Controls.Count - 1] as GridViewRow; // refer to PagerTemplate
GridViewRow pagerRow = theGrid.BottomPagerRow; //GridView较DataGrid提供了更多的API,获取分页块可以使用BottomPagerRow 或者TopPagerRow,当然还增加了HeaderRow和FooterRow
//updated at 2006年6月21日3:15:33

if (null != pagerRow) {
txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox; // refer to the TextBox with the NewPageIndex value
}

if (null != txtNewPageIndex) {
newPageIndex = int.Parse(txtNewPageIndex.Text) - 1; // get the NewPageIndex
}
}
else { // when click the first, last, previous and next Button
newPageIndex = e.NewPageIndex;
}

// check to prevent form the NewPageIndex out of the range
newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;

// specify the NewPageIndex
theGrid.PageIndex = newPageIndex;

// rebind the control
// in this case of retrieving the data using the xxxDataSoucr control,
// just do nothing, because the asp.net engine binds the data automatically
}
注意到,上面的示例中,由于增加了一个跳转按钮GO,但是asp。net不支持相关的CommandArgument值,虽然可以将Go Button的Commandname设为Page,还需要手动的在PageIndexChanging增加部分处理逻辑。


为什么只有使用数据源才能实现此功能,我用后台代码绑定数据功能实现不鸟。。。哪为高手指导一下
...全文
77 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
luozhuang2009 2009-07-14
  • 打赏
  • 举报
回复
能不能给个例子啊?我想实现从本页跳到另一个页面然后返回时会保持当前页
whxwchg 2009-07-14
  • 打赏
  • 举报
回复
不用自带的分页就好了,网上有好多自己的分页控件,AspNetPager等等。效率比自带的好多了
1.项目代码均经过功能验证ok,确保稳定可靠运行。欢迎下载体验!下载完使用问题请私信沟通。 2.主要针对各个计算机相关专业,包括计算机科学、信息安全、数据科学与大数据技术、人工智能、通信、物联网等领域的在校学生、专业教师、企业员工。 3.项目具有丰富的拓展空间,不仅可作为入门进阶,也可直接作为毕设、课程设计、大作业、初期项目立项演示等用途。 4.当然也鼓励大家基于此进行二次开发。在使用过程中,如有问题或建议,请及时沟通。 5.期待你能在项目中找到乐趣和灵感,也欢迎你的分享和反馈! 【资源说明】 C#开发基于FreeSql多库分布式事务、跨库查询、跨库分页查询、跨库增删改等功能实现源码+项目说明+sln.zip **前言** 话说2021年开始了一个基于ASP.NET Core 微服务的项目,谈到微服务 多库环境下 分布式事务、分库分表这些问题都是逃不开的,于是首先从ORM开始调研,需要考虑到一些重要的因素 **功能强大、支持多种数据库(并且行为一致,防止出现换库的情况)、支持分库分表** 等等,这时候第一时间就想到了 [FreeSql](https://github.com/dotnetcore/FreeSql) ,FreeSql的架构设计非常好,每一种支持的数据库都有对应的Provider实现 做到行为一致,而且支持CodeFirst和DbFirst,分库分表FreeSql也有比较简单切有效的方案,本人也经常向FreeSql的作者叶老板请教学习,非常佩服他的技术与人品,也非常感谢他能做出这么好的ORM框架。 **分布式事务** 既然分库了 分布式事务怎么处理,说到分布式事务 常见的解决方案有TCC/SAGA/消息队列最终一致性,在.NET生态中有基于消息队列实现的分布式事务 [CAP](https://github.com/dotnetcore/CAP) ,TCC和SAGA调研了很久没有发现有比较成熟的实现,那么就决定使用`CAP(最终一致性事务)` 由于项目持续的改版,业务的实时性变得越来越高,基于消息队列的这种最终一致性或者说异步事务的方案 越来越不适合我们的项目,这时候就需要同步的事务方案,TCC/SAGE又没有太好的解决方案(我真的没有找到。。),于是想着自己设计一个,基于FreeSql实现事务管理器。 想要的效果:和单库事务一样,出现错误回滚 但是问题来了 多库呢?不同的数据库呢? * 在多库事务的开启时,每个库管理开启自己的事务 * 如果某一个库事务开启后的操作出现异常,则回滚全部数据库事务 * 在多库事务提交时,每个库的事务统一提交 * 记录日志,第一个执行Common的数据库称之为主库,会自动创建一个日志表,用于记录多库事务的信息、执行的SQL、业务模块 用于人工介入或者事务补偿 * 如果主库(第一个库)Common成功后,其他某一个库可能由于网络原因、数据库宕机 无法Common事务,导致数据不一致,这时候要根据日志进行事务补偿或者人工介入,例如 存在三个库(订单库、物流库、商品库) 订单库就是主库(会记录日志) 在Common事务时,如果订单库(主库)Common失败,则(订单库、物流库、商品库)事务全部回滚,如果`订单库`(主库)Common成功,但是`物流库`由于其他原因无法Common成功 则会被日志记录并跳过,然后再去Common `商品库` 以及其他库.. **跨库查询/跨库分页查询** 通过时间分片定位、事件委托、分页算法实现跨库分页查询 1.appsettings.json配置 2.初始化数据库 3.获取IFreeSql操作对象 5.跨库分页查询 6. 跨库增删改 7.跨库并行查询(不分页) 8.跨库ToOne查询 9.跨库Any查询 10.分布式事务、多库事务

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧