首页 > 代码库 > GridView分页(第一页、上一页、下一页、最后页、当前页数/总页数)

GridView分页(第一页、上一页、下一页、最后页、当前页数/总页数)


<PagerTemplate>
                <table width="100%">
                    <tr>
                        <td width="75%">
                            <asp:ImageButton ID="imgBtnFirst" runat="server" CommandArgument="First" CommandName="Page"
                                ImageUrl="~/Images/First.gif" ToolTip="第一页" />
                            <asp:ImageButton ID="imgBtnPrev" runat="server" CommandArgument="Prev" CommandName="Page"
                                ImageUrl="~/Images/Previous.gif" ToolTip="上一页" />
                            <asp:ImageButton ID="imgBtnNext" runat="server" CommandArgument="Next" CommandName="Page"
                                ImageUrl="~/Images/Next.gif" ToolTip="下一页" />
                            <asp:ImageButton ID="imgBtnLast" runat="server" CommandArgument="Last" CommandName="Page"
                                ImageUrl="~/Images/Last.gif" ToolTip="最后页" />
                        </td>
                        <td align="right" width="25%">
                            页数:<asp:Label ID="lblCurrentPage" runat="server"></asp:Label>
                        </td>
                    </tr>
                </table>
            </PagerTemplate>

后台绑定事件:

 


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            #region 这里主要是换一种分页方式
            // 取得显示页数的那一列。
            GridViewRow pagerRow = GridView1.BottomPagerRow;
            if (pagerRow != null)
            {
                // 取得显示目前所在页数的 Label 控件。
                Label pageLabel = (Label)(pagerRow.Cells[0].FindControl("lblCurrentPage"));

                // 取得 第一页、上一页、下一页、最后页 的按钮。
                ImageButton imgBtnFirst =
                  (ImageButton)(pagerRow.Cells[0].FindControl("imgBtnFirst"));
                ImageButton imgBtnPrev =
                  (ImageButton)(pagerRow.Cells[0].FindControl("imgBtnPrev"));
                ImageButton imgBtnNext =
                  (ImageButton)(pagerRow.Cells[0].FindControl("imgBtnNext"));
                ImageButton imgBtnLast =
                  (ImageButton)(pagerRow.Cells[0].FindControl("imgBtnLast"));

                // 设定何时应该显示 第一页、上一页、下一页、最后页 的按钮。
                if (GridView1.PageIndex == 0)
                {
                    imgBtnFirst.Style.Add(HtmlTextWriterStyle.Visibility, "hidden");
                    imgBtnPrev.Style.Add(HtmlTextWriterStyle.Visibility, "hidden");
                }
                else if (GridView1.PageIndex == GridView1.PageCount - 1)
                {
                    imgBtnLast.Style.Add(HtmlTextWriterStyle.Visibility, "hidden");
                    imgBtnNext.Style.Add(HtmlTextWriterStyle.Visibility, "hidden");
                }
                else if (GridView1.PageCount <= 0)
                {
                    imgBtnFirst.Style.Add(HtmlTextWriterStyle.Visibility, "hidden");
                    imgBtnPrev.Style.Add(HtmlTextWriterStyle.Visibility, "hidden");
                    imgBtnNext.Style.Add(HtmlTextWriterStyle.Visibility, "hidden");
                    imgBtnLast.Style.Add(HtmlTextWriterStyle.Visibility, "hidden");
                }

                if (pageLabel != null)
                {
                    // 计算目前所在的页数。
                    int currentPage = GridView1.PageIndex + 1;

                    pageLabel.Text = currentPage.ToString() +
                        " / " + GridView1.PageCount.ToString();
                }
            }
            #endregion

        }


bind_gv()是
DataTable dt = new DataTable();
//dt = ClsDataBase.ExecuteNonQuery("select * from test");//这里放你的查询方法
this.GridView1.DataSource = dt;
this.GridView1.DataBind();

(出处:http://blog.csdn.net/zhaoqiliang527/archive/2009/02/15/3893312.aspx)

GridView分页(第一页、上一页、下一页、最后页、当前页数/总页数)