GridView 手写分页 初级

今天研究这个研究了半天,初步了解了GridView自动分页是如何操作的 话不多 贴代码、

 <PagerTemplate>
                    <asp:LinkButton ID="lbFirst" runat="server" CausesValidation="False" CommandArgument="First"
                        CommandName="Page">First</asp:LinkButton>
                    <asp:LinkButton ID="lbPrev" runat="server" CausesValidation="False" CommandArgument="Prev"
                        CommandName="Page">Prev</asp:LinkButton>
                    <asp:LinkButton ID="lbNext" runat="server" CausesValidation="False" CommandArgument="Next"
                        CommandName="Page">Next</asp:LinkButton>
                    <asp:LinkButton ID="lbLast" runat="server" CausesValidation="False" CommandArgument="Last"
                        CommandName="Page">Last</asp:LinkButton>
                    第<asp:Label ID="Label2" runat="server" Text="<%#((GridView)Container.Parent.Parent).PageIndex + 1 %>"></asp:Label>页
                    共<asp:Label ID="Label1" runat="server" Text="<%# ((GridView)Container.Parent.Parent).PageCount %>"></asp:Label>页
                    跳到<asp:TextBox ID="tbPage" runat="server" Text="<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>"
                        Width="27px"></asp:TextBox>
                    <asp:LinkButton ID="lbGO" runat="server" CausesValidation="False" CommandArgument="0"
                        CommandName="Page" Text="GO"></asp:LinkButton>
 </PagerTemplate>

//添加PageIndexChanging事件  注意:这个是必须的

 protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView gvw = (GridView)sender;
        if (e.NewPageIndex < 0)
        {
            TextBox pageNum = (TextBox)gvw.BottomPagerRow.FindControl("tbPage");
            int Pa = int.Parse(pageNum.Text);
            if (Pa <= 0)
                gvw.PageIndex = 0;
            else
                gvw.PageIndex = Pa - 1;
        }
        else
        {
            gvw.PageIndex = e.NewPageIndex;
        }
        GridView_Bind();
    }

<%#((GridView)Container.Parent.Parent).PageIndex + 1 %>  显示当前页

原文地址:https://www.cnblogs.com/Kiros/p/1886871.html