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增加部分处理逻辑。

自定义GridView分页模板相关推荐

  1. Gridview分页模板

    通过<PagerTemplate>--</PagerTemplate>来实现分页.代码如下: <PagerTemplate>           <div i ...

  2. GridView分页的实现以及自定义分页样式功能实例

    http://www.jb51.net/article/39677.htm GridView分页的实现 要在GridView中加入 //实现分页 AllowPaging="true" ...

  3. html tabcontrol模板,自定义TabControl 分页标题样式

    首先设置DrawMode 属性 tabControl.DrawMode = System.Windows.Forms.TabDrawMode.OwnerDrawFixed; tabControl.Dr ...

  4. java分页模板_java 分页模型的模板

    分页sql select top 每页要显示的记录数 * from 表名 where 主键 not in (select top (每页显示的记录数*(当前页-1)) 主键 from 表名 ) sel ...

  5. Asp.net GridView分页功能的实现

    最近学习了Asp.net的GridView分页的实现,当然,GridView自带分页功能,但是这个分页功能的后台数据库操作的时候仍是 需要查询出所有的记录,只是前台页面显示GridView的时候有一种 ...

  6. 组策略(八)使用使用自定义RealVNC adm模板对域内工作站设置统一标准设置

    VNC是网络远程管理的利器.VNC软件主要由两个部分组成:VNC Server及VNC Viewer.用户需先将VNC Server安装在被控端的计算机上后,才能在主控端执行VNC Viewer控制被 ...

  7. 自定义Word样式模板,设置为默认模板

    文章目录 前言 一.具体操作步骤 前言 由于在大学毕业写论文的时候,论文格式的要求十分严格,但是在完成论文编辑之后,发现这个论文的格式样式对于今后word排版大有益处,于是就将该论文的格式样式保存在一 ...

  8. 30、自定义gridview

    要想实现自定义gridview效果,有下边几个步骤: 1.定义grivew中的item的xml文件 假如item需要显示一个图片,图片下边显示文字: 1 <?xml version=" ...

  9. php自定义函数数学计算,ThinkPHP自定义函数解决模板标签加减运算的方法

    本文实例讲述了ThinkPHP自定义函数解决模板标签加减运算的方法.分享给大家供大家参考.具体如下: 实际项目中,我们经常需要标签变量加减运算的操作.但是,在ThinkPHP中,并不支持模板变量直接运 ...

最新文章

  1. apply call bind 简介
  2. shllter自动和手动实例
  3. java implements interface_Java 接口 interface implements
  4. Zedboard学习(八):zedboard移植opencv
  5. Introduction to algrithms exercise2.3-7
  6. 部署eureka和config
  7. @ngrx/router-store 在 SAP 电商云 Spartacus UI 开发中的作用
  8. 香港印象:维多利亚港湾·张学友的手印
  9. 一张图看懂单机/集群/热备/磁盘阵列(RAID)
  10. Guava入门~CharMatcher
  11. IO流和集合的交集Properties类的介绍。
  12. 3dmax高版本转低版本插件_Fundebug前端JavaScript插件更新至1.8.0,兼容低版本的Android浏览器...
  13. 【个人笔记】OpenCV4 C++ 快速入门 25课
  14. 如何系统地学习深度学习(从初级到高级,初学者必看)
  15. tomcat + apache组合配置
  16. 欧姆龙NX控制器与Sysmac studio的电机配置方法
  17. abaqus python教程_Abaqus-python脚本到底应该怎么写?一文带你入门
  18. JDK 运行参数 JAVA -Dxxx与System.setProperty()的关系
  19. 类iGoogle实现参考资料
  20. 二级域名分发系统美化版网站源码 附搭建教程

热门文章

  1. jsp与java的关系_jsp教程中:jsp与servlet的区别和关系
  2. java将按钮添加到指定位置_在运行时将按钮添加到布局
  3. Kdtree(K-dimension tree)学习
  4. HDU - 3078 Network 倍增LCA
  5. java 宽字节_宽字节注入
  6. xk3190串口通讯JAVA开发包_常用品牌plc通讯协议汇总学习
  7. 在windows下配置pthread多线程
  8. MathType6.9b安装及在Word2013中无法正常使用的解决方法
  9. ROS Melodic安装、配置和使用turtlebot2(集成众多源代码直接下载)
  10. shell设置系统环境变量的问题