好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

高效分页控件(开源)再加自动恢复位置控件全部开源

高效分页控件(开源)再加自动恢复位置控件全部开源

在asp.net开发中经常碰到分页的问题。找了各种各样的分页控件,要么是有问题,要么是现实不了我想要的功能,要么太复杂,或者要收费。一怒之下自己写了个分页的控件(花了好多个小时才给整出的),欢迎大家拍砖。经过测试对比,结果比常见的分页控件性能要好。而且使用很简单。有问题或者bug欢迎反应或者留言,最好的把demo程序发给我。我会在这个页面及时更新。谢谢。

源代码下载

附上几张截图:

由于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;

作者: Leo_wl

    

出处: http://www.cnblogs.com/Leo_wl/

    

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

版权信息

查看更多关于高效分页控件(开源)再加自动恢复位置控件全部开源的详细内容...

  阅读:48次