前台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_DataBound(object sender, EventArgs e)
{
// 取得显示页数的那一列。
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();
}
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
bind_gv();
}