前台代码:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
            AutoGenerateColumns="False" DataKeyNames="ScoreID"
            DataSourceID="SqlDataSource1" onpageindexchanging="GridView1_PageIndexChanging" 
            Width="100%" EmptyDataText="没有数据记录!"
            BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px"
            CellPadding="3" GridLines="Horizontal">
            <AlternatingRowStyle BackColor="#F7F7F7" />
            <Columns>
                <asp:BoundField DataField="ScoreID" HeaderText="成绩编号" ReadOnly="True"
                    SortExpression="ScoreID" />
                <asp:BoundField DataField="CourseID" HeaderText="课程编号"
                    SortExpression="CourseID" />
                <asp:BoundField DataField="StuScore" HeaderText="学生成绩"
                    SortExpression="StuScore" />
                <asp:BoundField DataField="StuID" HeaderText="学生编号" SortExpression="StuID" />
            </Columns>
            <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
            <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
            <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
            <PagerTemplate>
                当前第:
               <%-- //((GridView)Container.NamingContainer)就是为了得到当前的控件--%>
                <asp:Label ID="LabelCurrentPage" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageIndex + 1 %>"></asp:Label>
                页/共:
               <%-- //得到分页页面的总数--%>
                <asp:Label ID="LabelPageCount" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageCount %>"></asp:Label>
                页
                 <%--//如果该分页是首分页,那么该连接就不会显示了.同时对应了自带识别的命令参数CommandArgument--%>
                <asp:LinkButton ID="LinkButtonFirstPage" runat="server" CommandArgument="First" CommandName="Page"
                    Visible='<%#((GridView)Container.NamingContainer).PageIndex != 0 %>'>首页</asp:LinkButton>
                <asp:LinkButton ID="LinkButtonPreviousPage" runat="server" CommandArgument="Prev"
                    CommandName="Page" Visible='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>'>上一页</asp:LinkButton>
               <%--//如果该分页是尾页,那么该连接就不会显示了--%>
                <asp:LinkButton ID="LinkButtonNextPage" runat="server" CommandArgument="Next" CommandName="Page"
                    Visible='<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>'>下一页</asp:LinkButton>
                <asp:LinkButton ID="LinkButtonLastPage" runat="server" CommandArgument="Last" CommandName="Page"
                    Visible='<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>'>尾页</asp:LinkButton>
                转到第
                <asp:TextBox ID="txtNewPageIndex" runat="server" Width="20px" Text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>' />页
                <%--//这里将CommandArgument即使点击该按钮e.newIndex 值为3 --%>
                <asp:LinkButton ID="btnGo" runat="server" CausesValidation="False" CommandArgument="-2"
                    CommandName="Page" Text="GO" />
            </PagerTemplate>
            <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
            <SortedAscendingCellStyle BackColor="#F4F4FD" />
            <SortedAscendingHeaderStyle BackColor="#5A4C9D" />
            <SortedDescendingCellStyle BackColor="#D8D8F0" />
            <SortedDescendingHeaderStyle BackColor="#3E3277" />
        </asp:GridView>

后台代码:

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)

{

GridView theGrid = sender as GridView;

int newPageIndex = 0;

if (e.NewPageIndex == -3)

{

//点击了Go按钮

TextBox txtNewPageIndex = null;

//GridView较DataGrid提供了更多的API,获取分页块可以使用BottomPagerRow 或者TopPagerRow,当然还增加了HeaderRow和FooterRow

GridViewRow pagerRow = theGrid.BottomPagerRow;

if (pagerRow != null)

{

//得到text控件

txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;

}

if (txtNewPageIndex != null)

{

//得到索引

newPageIndex = int.Parse(txtNewPageIndex.Text) - 1;

}

}

else

{

//点击了其他的按钮

newPageIndex = e.NewPageIndex;

}

//防止新索引溢出

newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;

newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;

//得到新的值

theGrid.PageIndex = newPageIndex;

//重新绑定

GridView1.DataBind();

}

转载于:https://www.cnblogs.com/JCH344/p/6029201.html

gridview实现分页相关推荐

  1. showModalDialog 页面上GridView的分页问题

    2019独角兽企业重金招聘Python工程师标准>>> 在showModalDialog打开的页面上有个GridView控件,GridView中有分页导航,点击导航按钮时,会在sho ...

  2. GridView自定义分页样式(上一页,下一页,到第几页)(新手教程)

    今天要为网站做一个文章列表,发现GridView的分页样式很难看,于是结合网上的例子,自己做了一个.不是很美观,不过还是很实用的,先看下效果吧,如图(1).演示地址http://www.veryam. ...

  3. GridView自定义分页

    第一步:设置GridView的AllowPaging="True" 和 PageSize="20" 第二步:创建GridView的分页模板 <PagerT ...

  4. GridView自定义分页导航

    自己做的一个项目中所运用到的技术:| 1.         日历控件(带时分秒) 2.         GridView 批量删除,自定义分页,定位页码 3.         GridView 修改 ...

  5. GridView,Repeater分页控件:WebPager(开源)

    一.特点: 可以用在母板.MS Ajax等控件嵌套的任何地方,支持GridView,Repeater等数据控件的分页. 二.属性: 1. PagerStyle: 设置分页样式(NextPrev,Num ...

  6. gridview 万能分页代码

    实现如下图片的分页:第[]页,共[]页,首页,上一页,下一页,末页 跳到某页:图片在最下面:gv里面的模板PagerTemplate复制到你的gv对应位置,然后在 protected void gvg ...

  7. GridView 通用分页

    在项目中,遇到一个分页问题,原理很简单,但是,为了达到复用,将分页的控件和方法封装到用户控件中,这样就可以将分页的控件.代码与UI层分离. 以达到代码的复用好封装.代码清单如下: GridViewPa ...

  8. 利用aspnetPager控件加存储过程实现对gridview的分页

    gridview是asp.net常用的显示数据控件,对于.net开发人员来说应该是非常的熟悉了.gridview自带有许多功能,包括分页,排序等等,但是作为一个.net开发人员来说熟练掌握利用存储过程 ...

  9. C# GridView的分页使用 附代码

    这里没有使用数据库,仅仅是将得到数据进行分页显示. 1.先启用编辑列,对列数进行编辑.我这里有8个列.如果列数没有设计好,后面代码运行时,会报错. 2.相关代码如下,这里测试了http访问后台数据,j ...

最新文章

  1. php函数get_magic_quotes_gpc详解
  2. 无事“自动驾驶”,有事“辅助驾驶”?
  3. 一种电子病历系统软件框架思想
  4. 修改thymeleaf默认路径
  5. 微软老兵 Antoine LeBlond 将正式离职
  6. php 变量 换行,php去除换行符的方法小结(PHP_EOL变量的使用)_PHP教程
  7. EL与JSTL表达式代码示例
  8. Linux:网络安全与主机基本防护:限制端口, 网络升级与 SELinux
  9. HTTP缓慢拒绝服务攻击
  10. 湖北省荆州市谷歌高清卫星地图下载
  11. 多媒体计算机辅助英语教学,浅谈多媒体计算机辅助英语教学
  12. MySQL查询之内存临时表
  13. 阅读笔记04——魔鬼搭讪学
  14. Mybatis-Plus 新手入门,一篇足以
  15. 利用java编写网络聊天程序并加密信息
  16. 计算机配置打开命令行,win7开机f8进入修复计算机cmd装xp
  17. 随机森林(RandomForest,RF)网格搜索法调参
  18. java:计算自由落体运动中物体的位移
  19. 工作效率的变速器——沟通
  20. Unity中根据矩阵判断一个点在自身的哪一侧

热门文章

  1. Java 面向对象的程序设计(二)
  2. React笔记-事件分发
  3. iOS中 Animation 动画大全 韩俊强的博客
  4. 4个常用的awk统计命令
  5. 最常用的动态sql语句梳理Mybatis(转)
  6. java.util.ConcurrentModificationException异常
  7. web前端url传递值 js加密解密
  8. SQL Server 2008之DMF
  9. 安装office时,提示某项注册表无法写入,请查看是否有管理员权限
  10. 《WinForm开发系列之高级篇》Item2 TCP异步传输