20150410---GridView分页(备忘)

GridView自带分页功能,但是模板单一,试用较少数据。

AllowPaging="true"

PageSize="10"

启用分页设置。默认每页数量为10

添加分页触发事件:
OnPageIndexChanging="GridView1_PageIndexChanging"

<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 %>‘ BorderStyle="None" />页
<%--//这里将CommandArgument即使点击该按钮e.newIndex 值为3--%>
    <asp:LinkButton ID="btnGo" runat="server" CausesValidation="False" CommandArgument="-2" CommandName="Page" Text="GO"/>

</PagerTemplate>

下面是添加分页触发事件(C#后台代码):

GridView1_PageIndexChanging

protected void GridView2_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;

        //重新绑定
        bind();
    }

最终分页样式:

时间: 2024-08-09 05:10:07

20150410---GridView分页(备忘)的相关文章

jade分页备忘

涉及到 limit,skip,total   div.pagination       ul         li            a(href="/admin/redpack/list?limit=#{data.limit}&skip=0") First         if data.skip*1-data.limit*2>0           li              a(href="/admin/redpack/list?limit=#{d

linux下常用命令备忘

转自:Linux 命令集锦 linux下查看监听端口对应的进程 # lsof -i:9000 # lsof -Pnl +M -i4 如果退格键变成了:"^h". 终端连接unix删除退格键,按住CTL键同时按delete Linux搜索 # find / -name "xxx.conf" 查看linux是32位还是64位的命令 #file /sbin/init #getconf LONG_BIT #getconf -a 在Linux和Windows下都可以用nslo

PHP设计模式之备忘模式

1.Norton Ghost的方便与问题 我们大多数win的用户都用过Norton Ghost,只要将目前系统备份一下生成镜像文件,等系统中毒或崩溃的时候,用Norton Ghost恢复一下就回到备份时候的样子了. 这个可以说就是备忘(Memento)模式的基本原理了,先备份,需要的时候恢复.因此备忘模式是比较好理解的. 但在实际应用中,如何正确的应用备忘模式,是需要注意的. 难道我们在word写文章的时候,先要Ghost备份一下所有的硬盘,一旦文章写错了,需要恢复,就用Ghost覆盖硬盘? 就

Phalcon 知识点备忘

phalcon 毕竟无法看到源代码,有些小设置还是需要实践记忆. 一.如何调用config.php里面的配置 很多时候,我们习惯将全局的配置参数放入主配置文件中,比如分页数.比如appkey之类. 'params' => array(  //加入config.php中,那controller中如何调用呢 'limit' => 10, ) 想要在controller中调用,可以通过services.php $di->set('main_config', $config); //将配置文件作

AngularJS之备忘与诀窍

译自:<angularjs> 备忘与诀窍 目前为止,之前的章节已经覆盖了Angular所有功能结构中的大多数,包括指令,服务,控制器,资源以及其它内容.但是我们知道有时候仅仅阅读是不够的.有时候,我们并不在乎那些功能机制是如果运行的,我们仅仅想知道如何用AngularJS去做实现一个具体功能. 在这一章中,我么视图给出完整的样例代码,并且对这些样例代码仅仅给出少量的信息和解释,这些代码解决是我们在大多数Web应用中碰到的通用问题.这些代码没有具体的先后次序,你尽可以跳到你关心的小节先睹为快或者

AnjularJS ngTable插件使用备忘记录(一个ngTableDemo)

项目中用到AngularJS的表格ng-table,功能相当强大,基本的排序.分页等都有.这里做个备忘,以便以后使用. 该Demo利用vs2012中的webapi(暂时没连数据库),下面是详细代码 ngTable.html <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" con

MySQL使用备忘的实际操作步骤概述

以下的文章主要向大家介绍的是MySQL使用备忘的实际操作步骤,随着MySQL数据库的广泛应用随之MySQL相关的实际应用也越来越受重视,以下就是文章的具体内容描述,望你能有所收获. 使MySQL写日志 编辑my.ini [MySQLd] log MySQL用limit分页 select * from tablename limit offset,rowcount,offset从0开始,rowcount是返回的条数 忘记密码 1.用任务管理器kill掉MySQL进程 2.执行MySQLd --sk

RxJava & RxAndroid备忘

"你问我要去向何方,我指着大海的方向" 今天在刷G+的时候看到Dave Smith推荐了一个视频 <Learning RxJava (for Android) by example> 点进去看了一下,原来是位熟悉的"阿三哥",视频封面如下:(没有歧视的意思,不要喷我啊~,为什么感到熟悉?接着往下看) 几乎同时也看到了JetBrains在G+也推荐了篇在Medium上的博文 <RxAndroid And Kotlin (Part 1)> ,然后

工作备忘:cacti&nagios登录密码修改方法

[[email protected]]# mysql -u root -p mysql> use cacti; mysql> select * from user_auth; mysql> update user_auth set password=md5("cactipasswd") where id='1'; 现在cacti登录的新密码就是cactipasswd [[email protected]]# /usr/bin/htpasswd /usr/local/n

备忘-linux文件系统结构

用apache的时候总是要进入/var/www, 用久了开始好奇这些个目录都是派什么用处的,简单整理了一下 /bin 存放二进制命令文件,这个目录下面不允许存在子目录/boot bootloader的静态文件,当然OS的文件也必须在这里/dev 设备文件,MAKEDEV命令可以创建设备/etc 特定主机的配置文件,必须是静态文件,非可执行文件: opt, X11, sgml, xml/home 用户目录 /lib 存放主要的共享库和核心模块/media 可移除媒体的挂载点: floppy, cd