GridView DropDownList Pager(转)
转自:http://weblogs.asp.net/rajbk/archive/2006/08/14/GridView-DropDownList-Pager.a
This post shows you how to add a custom DropDownlist pager and pager buttons to the GridView as shown below:
Unlike the behavior of the default pager buttons, these buttons get disabled instead of being hidden (depending on the state of the GridView).
The code is pretty straightforward.
Add the following pager template to your GridView.
<asp:DropDownList ID="ddlPageSelector" runat="server" AutoPostBack="true">
<asp:Button Text="First" CommandName="Page" CommandArgument="First" runat="server"
<asp:Button Text="Previous" CommandName="Page" CommandArgument="Prev" runat="server"
<asp:Button Text="Next" CommandName="Page" CommandArgument="Next" runat="server"
<asp:Button Text="Last" CommandName="Page" CommandArgument="Last" runat="server"
Add a RowCreated event handler to your GridView and add the following code:
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) {
if (e.Row.RowType == DataControlRowType.Pager) {
PresentationUtils.SetPagerButtonStates(GridView1, e.Row, this);
Finally, add the following code to your common class (PresentationUtils in my case)
public static void SetPagerButtonStates(GridView gridView, GridViewRow gvPagerRow, Page page) {
int pageIndex = gridView.PageIndex;
int pageCount = gridView.PageCount;
Button btnFirst = (Button)gvPagerRow.FindControl("btnFirst");
Button btnPrevious = (Button)gvPagerRow.FindControl("btnPrevious");
Button btnNext = (Button)gvPagerRow.FindControl("btnNext");
Button btnLast = (Button)gvPagerRow.FindControl("btnLast");
btnFirst.Enabled = btnPrevious.Enabled = (pageIndex != 0);
btnNext.Enabled = btnLast.Enabled = (pageIndex < (pageCount - 1));
DropDownList ddlPageSelector = (DropDownList)gvPagerRow.FindControl("ddlPageSelector");
ddlPageSelector.Items.Clear();
for (int i = 1; i <= gridView.PageCount; i++) {
ddlPageSelector.Items.Add(i.ToString());
ddlPageSelector.SelectedIndex = pageIndex;
//Anonymous method (see another way to do this at the bottom)
ddlPageSelector.SelectedIndexChanged += delegate {
protected void ddlPageSelector_SelectedIndexChanged(object sender, EventArgs e) {
GridView1.PageIndex = ((DropDownList)sender).SelectedIndex;