使用AspNetPager与GridView完成分页
使用AspNetPager与GridView完成分页
由于GridView的分页功能实在是太弱了,所以需要使用强大的AspNetPager来作为分页控件。最简单的办法就是GridView控件下面接着放一个AspNetPager控件,但是这样好像就不能用GridView的分页功能了。在数据量不大的情况下,使用GridView的分页是十分方便有效的。另外还有一个问题就是分页控件在GridView生成的表格的下面,而没有像GridView自带分页那样包含到表格内部,这点也不是很爽。
要解决以上的问题,可以将AspNetPager放入GridView的分页模板(PagerTemplate)中,如下代码所示:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowPaging="True">
<Columns>
<asp:BoundField DataField="DmId" HeaderText="序号" ReadOnly="True" SortExpression="DMID" />
……
……
</Columns>
<PagerStyle HorizontalAlign="Center" />
<PagerTemplate>
<asp:AspNetPager ID="AspNetPager1" runat="server" ShowBoxThreshold="5" ShowPageIndexBox="Auto" CenterCurrentPageButton="True">
</asp:AspNetPager>
</PagerTemplate>
</asp:GridView>
但是这样做要解决几个问题:
(1)这个GridView每一页的行数AspNetPager并不知道。解决办法:为AspNetPager添加属性PageSize="<%# ((GridView)Container.NamingContainer).PageSize%>"
(2)这个GridView绑定的总记录数AspNetPager也不知道。解决办法:为AspNetPager添加属性RecordCount="<%#((IList)(((GridView)Container.NamingContainer).DataSource)).Count %>"
(3)这个GridView当前在第几页AspNetPager也不知道。这个问题的解决可不像前面那么简单了,通过设置属性CurrentPageIndex的方式AspNetPager根本不认!(估计是AspNetPager的一个Bug吧)要解决这个问题就只有在每次翻页时后台代码中为AspNetPager设置CurrentPageIndex属性。
(4)使用AspNetPager后GridView并不会触发PageChanging事件。但是要触发AspNetPager的PageChanging事件,所以可以为分页模板中的AspNetPager控件添加事件处理:OnPageChanging="AspNetPager1_PageChanging",对应的就是分页的后台代码:
protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex - 1;//这儿需要注意,AspNetPager中的PageIndex是从1开始的,而GridView的是从0开始的,所以要减1.
Bind();//GridView的数据绑定方法
Wuqi.Webdiyer.AspNetPager pager = this.GridView1.BottomPagerRow.FindControl("AspNetPager1") as Wuqi.Webdiyer.AspNetPager;
pager.CurrentPageIndex = e.NewPageIndex;//这里就是为了解决前面的第3个问题。
}
OK,以上4个问题都解决了,我们的GridView+AspNetPager的分页就完成了!另外如果觉得AspNetPager的样式不好看可以再定义一下CSS。最后完整的代码是:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowPaging="True">
<Columns>
<asp:BoundField DataField="DmId" HeaderText="序号" ReadOnly="True" SortExpression="DMID" />
……
……
</Columns>
<PagerStyle CssClass="GridViewPager" HorizontalAlign="Center" />
<PagerTemplate>
<asp:AspNetPager ID="AspNetPager1" runat="server" ShowBoxThreshold="5" ShowPageIndexBox="Auto"
PageSize="<%# ((GridView)Container.NamingContainer).PageSize%>" OnPageChanging="AspNetPager1_PageChanging"
RecordCount="<%#((IList)(((GridView)Container.NamingContainer).DataSource)).Count %>"
CenterCurrentPageButton="True">
</asp:AspNetPager>
</PagerTemplate>
</asp:GridView>
//以下是后台代码
protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex - 1;
Bind();
Wuqi.Webdiyer.AspNetPager pager = this.GridView1.BottomPagerRow.FindControl("AspNetPager1") as Wuqi.Webdiyer.AspNetPager;
pager.CurrentPageIndex = e.NewPageIndex;
}
使用AspNetPager与GridView完成分页的更多相关文章
- GridView自定义分页样式(上一页,下一页,到第几页)
今天要为网站做一个文章列表,发现GridView的分页样式很难看,于是结合网上的例子,自己做了一个.不是很美观,不过还是很实用的,先看下效果吧,如图(1). 图(1)GridView分页效果 自定义G ...
- ASP.NET真分页_接前篇引用AspNetPager.dll进行数据分页
一.前端准备工作 1.之前我写到过<Asp.net中引用AspNetPager.dll进行数据分页> 这种分页方式只能在前台将数据分页,而每次点击查询时对目标数据库还是全查询,这样不仅会 ...
- aspnetpager的2种分页方法
<webdiyer:AspNetPager ID="AspNetPager1" UrlPaging="True" PageSize="20&qu ...
- aspnetpager+repeater+oracle实现分页功能
一.设计原理阐述 数据查询分页,这个功能相信大家都很熟悉,通过数据库或其它数据源进行查询操作后,将获得的数据显示到界面上,但是由于数据量太大,不能一次性完全的显示出来,就有了数据分页的需求.这个需求在 ...
- GridView自定义分页
CSS样式 首先把CSS样式代码粘贴过来: .gv { border: 1px solid #D7D7D7; font-size:12px; text-align:center; } .gvHeade ...
- GridView 控制分页页码间距
来源:http://auv2009.blog.163.com/blog/static/68858712200992793853431/ 技巧1:在分页区中改变当前页码的样式或高亮显示页码 一个简单的办 ...
- 自编辑列的gridview,分页,删除,点击删除提示“确认”
分页: gridview的属性中:AllowPaging="True" PageSize="2" 找到gridview的PageIndexChan ...
- GridView的分页代码
1.前台代码 <PagerTemplate><div style="text-align:center; color:Blue"> <asp:Link ...
- 使用第三方分页AspNetPager实现真正分页的SQL原理
AspNetPager是一个第三方分页第三方控件,可以和数据绑定控件(GridView等)方便的结合,实现真分页. 真分页:从数据库中获取符合要求的部分数目的记录.性能较高,数据量小,网络负载小,对数 ...
随机推荐
- 自己动手用Javascript写一个无刷新分页控件
.NET技术交流群:337901356 ,欢迎您的加入! 对 于一个用户体验好的网站来说,无刷新技术是很重要的,无刷新,顾名思义,就是局部刷新数据,有用过Asp.net Web Form技术开发网页的 ...
- C# ado.net 使用 row_number over() 简单的分页示例
/// <summary> /// 获取Paging列表 /// </summary> public List<HousesAgentEntity> GetPage ...
- C#垃圾回收机制详解
一.托管代码/非托管代码 C#代码通过C#编译器编译成程序集,程序集由微软中间语言组成,CLR会为程序集开辟一个应用程序域,程序集就是运行在这个应用程序域里面的,应用程序域是相互独立的,互不影响. 托 ...
- C#中out和ref使用
1.out必须在方法中为其赋值,在调用的时候必须在变量的前面加上out关键字,侧重输出. 2.ref修饰方法的参数,在调用的时候必须在变量的前面加上ref关键字,可以修改其值也可以不修改,侧重修改. ...
- Conversion to Dalvik format failed:Unable toexecute dex: method ID not in [0, 0xffff]: 65536
关于方法数超限,Google官方给出的方案是这样的:https://developer.android.com/intl/zh-cn/tools/building/multidex.html 我也写过 ...
- Oracle 递归函数与等级
--基数数据1 SELECT ID, mt.materialtypename, mt.parenttypeid FROM material_type mt; 使用递归还是与LEVEL 1 SELECT ...
- Object-C类目(Category)
类目是Object-C中最有用的一个特性.实质上,类目允许你为一个已存在的类添加一些方法而不用子类化该类,也不需要你了解该类的实现细节. 这是特别有用的,因为你可以给一个内建的对象添加方法.当你想在你 ...
- 一个简单的定时器(NSTimer)的封装
在项目开发中我们有的时候需要用到计时器,比如登录超时,scrollview的滚动等,那么就让我们自己手动的去创建一个类库吧. 1 首先你需要一个向外提供创建的便捷方法. 1.1 这里考虑两种情况,一种 ...
- ArcGis for WPF(1)
这篇文章主要是讲窗体中怎么加载一张在线地图. 第一步:首先引用ESRI.ArcGIS.Client.dll类库. 第二步:在XAML中添加如下代码: <Window x:Class=" ...
- SomeThing of Memcache
Memcache for .net 文章一: http://blog.csdn.net/dinglang_2009/article/details/6917794 不定时更新