GrideView 分页

来源:互联网 发布:丁春秋和萧峰 知乎 编辑:程序博客网 时间:2024/06/05 11:28

 protected void btnFirst_Click(object sender, EventArgs e)

    {

 

        switch (((LinkButton)sender).CommandArgument.ToString())

        {

            case "first":

 

                GV_YiZhuCeSheBei.PageIndex = 0;

                break;

            case "last":

                GV_YiZhuCeSheBei.PageIndex = GV_YiZhuCeSheBei.PageCount - 1;

                break;

            case "prev":

 

                GV_YiZhuCeSheBei.PageIndex = (GV_YiZhuCeSheBei.PageIndex - 1) > 0 ? (GV_YiZhuCeSheBei.PageIndex - 1) : 0;

                break;

            case "next":

                GV_YiZhuCeSheBei.PageIndex = GV_YiZhuCeSheBei.PageIndex + 1;

                break;

            case "go":

                {

                    GridViewRow gvr = GV_YiZhuCeSheBei.BottomPagerRow;

                    TextBox temp = (TextBox)gvr.FindControl("txtNewPageIndex");

                    int res = Convert.ToInt32(temp.Text.ToString());

                    GV_YiZhuCeSheBei.PageIndex = res - 1;

                }

                break;

        }

        GV_YiZhuCeSheBei.DataBind();//根据需要重新绑定数据源至GridView控件。

    }

 

 

 

   <PagerTemplate>

                 <table width="100%"  style="font-size:12px;">

        <tr>

 

            <td style="text-align: right; width:50%;">

                &nbsp;</td>

            <td style="text-align: right; ">

                第<asp:Label ID="lblPageIndex" runat="server" Text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>'></asp:Label>页

            /共<asp:Label ID="lblPageCount" runat="server" Text='<%# ((GridView)Container.Parent.Parent).PageCount %>'></asp:Label>页&nbsp;&nbsp;</td>

            <td style="text-align: right; ">

                <asp:LinkButton ID="btnFirst" runat="server" CausesValidation="False" 

                    CommandArgument="first" CommandName="Page" OnClick="btnFirst_Click" Text="首页">

            </asp:LinkButton>

            </td>

            <td style="text-align: right; ">

                <asp:LinkButton ID="btnPrev" runat="server" CausesValidation="False" 

                    CommandArgument="prev" CommandName="Page" onclick="btnFirst_Click" Text="上一页">

            </asp:LinkButton>

            </td>

            <td style="text-align: right; ">

                <asp:LinkButton ID="btnNext" runat="server" CausesValidation="False" 

                    CommandArgument="next" CommandName="Page" OnClick="btnFirst_Click" Text="下一页">

            </asp:LinkButton>

            </td>

            <td style="text-align: right; ">

                <asp:LinkButton ID="btnLast" runat="server" CausesValidation="False" 

                    CommandArgument="last" CommandName="Page" OnClick="btnFirst_Click" Text="尾页">

            </asp:LinkButton>

            </td>

            <td>

             <asp:TextBox ID="txtNewPageIndex" runat="server" AutoPostBack="True" 

                    ontextchanged="txtNewPageIndex_TextChanged" 

                    Text="<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>" Width="20px"></asp:TextBox>

            </td>

            <td style="text-align: right; width:15px; ">               

                <asp:LinkButton ID="btnGo" runat="server" CausesValidation="False" 

                    CommandArgument="go" CommandName="Page" OnClick="btnFirst_Click" Text="GO"></asp:LinkButton>

            </td>

        </tr>

    </table>

            </PagerTemplate>