GridView分页使用

GridView控件自带分页,绑定数据源控件后allowPaging即可自动实现分页。此篇为代码实现分页方法。

PageLoad事件中指定允许分页和每页个数

GridView2.AllowPaging = true;
GridView2.PageSize = 5;

当需要使用自己的翻页界面布局GridView提供了编辑模板使用。在模板选择的下拉框中选中PagerTemplate即可自定义布局

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" OnPageIndexChanging="GridView2_PageIndexChanging"
        AllowPaging="true" PageSize="2" PageIndex="1">        
        <Columns>
            <asp:BoundField DataField="ID" HeaderText="ID" />
            <asp:BoundField DataField="Name" HeaderText="姓名" />
            <asp:BoundField DataField="Code" HeaderText="编码" />
            <asp:BoundField DataField="Level" HeaderText="等级" />
        </Columns>
        <PagerTemplate>
            当前第:
            <%--((GridView)Container.NamingContainer)就是为了得到当前的GridView控件--%>
            <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通常只在设置CommandName属性时使用,作为指定补充 CommandName 属性的参数,提供要执行的Command 事件处理程序的可选参数。例如 CommandName 属性设置为 Sort 并将 CommandArgument 属性设置为 Ascending,以指定按升序排序的命令。                
             --%>
             
            <%-- 首页按钮,对应了自带识别的命令参数CommandArgument--%>
            <asp:LinkButton ID="LinkButtonFirstPage" runat="server" CommandArgument="First" CommandName="Page"
                Visible='<%# GridView2.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" /><%--这里将CommandArgument即使点击该按钮e.newIndex 值为3 --%>
            <asp:LinkButton ID="btnGo" runat="server" CausesValidation="False" CommandArgument="-1"
                CommandName="Page" Text="GO" />
        </PagerTemplate>
    </asp:GridView>
View Code

其中代码

(GridView)Container.NamingContainer

用于在GridView的PagerTemplate中获取到GridView,以PageIndex属性判断按钮的显示和隐藏。

后台操作只需在PageIndexChanging事件中进行处理。

   private void Bind(string sqlStr)
    {  
        SqlData SqlData = new SqlData();
        DataTable dt = SqlData.GetDataTableFromDB(sqlStr);
        GridView2.DataSource = dt;
        GridView2.DataBind();
    }    

SqlData类为数据库连接类,获取一张数据表

protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {                                
        int newPageIndex = 0;
        if (e.NewPageIndex < 0)
        {
            //点击了Go按钮
            TextBox txtNewPageIndex = null;

            //GridView较DataGrid提供了更多的API,获取分页块可以使用BottomPagerRow 或者TopPagerRow,当然还增加了HeaderRow和FooterRow
            GridViewRow pagerRow = GridView2.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 >= GridView2.PageCount ? GridView2.PageCount - 1 : newPageIndex;

        //得到新的值
        GridView2.PageIndex = newPageIndex;

        //重新绑定
        string sqlStr = "SELECT [ID], [Name], [Code], [Level] FROM [Addr_OfficialCity]";
        this.Bind(sqlStr);
    }
View Code

即可完成分页,简洁明了

原文地址:https://www.cnblogs.com/candr/p/3812244.html