首先添加一个GridView的模板列
前台:
//
添加GridView模板列
< PagerTemplate >
< asp:Label ID = " CurrentPageLabel " ForeColor = " Blue " runat = " server " Text = " 当前页: " />& nbsp;
< asp:LinkButton id = " FirstPage " runat = " server " CommandArgument = " FIRST " CommandName = " Page " > 首页 </ asp:LinkButton >
< asp:linkbutton id = " PreviousPage " runat = " server " CommandArgument = " Prev " CommandName = " Page " > 上一页 </ asp:linkbutton >
< asp:linkbutton id = " NextPage " runat = " server " CommandArgument = " NEXT " CommandName = " Page " > 下一页 </ asp:linkbutton >
< asp:linkbutton id = " LastPage " runat = " server " CommandArgument = " LAST " CommandName = " Page " > 尾页 </ asp:linkbutton >
< asp:label id = " Lab3 " runat = " server " ForeColor = " Blue " Height = " 7px " Font - Size = " 12px " ></ asp:label >
< asp:Label ID = " MessageLabel " ForeColor = " Blue " Text = " 页码: " runat = " server " />
< asp:DropDownList ID = " PageDropDownList " AutoPostBack = " true " runat = " server " OnSelectedIndexChanged = " PageDropDownList_SelectedIndexChanged " />& nbsp;
</ PagerTemplate >
< PagerTemplate >
< asp:Label ID = " CurrentPageLabel " ForeColor = " Blue " runat = " server " Text = " 当前页: " />& nbsp;
< asp:LinkButton id = " FirstPage " runat = " server " CommandArgument = " FIRST " CommandName = " Page " > 首页 </ asp:LinkButton >
< asp:linkbutton id = " PreviousPage " runat = " server " CommandArgument = " Prev " CommandName = " Page " > 上一页 </ asp:linkbutton >
< asp:linkbutton id = " NextPage " runat = " server " CommandArgument = " NEXT " CommandName = " Page " > 下一页 </ asp:linkbutton >
< asp:linkbutton id = " LastPage " runat = " server " CommandArgument = " LAST " CommandName = " Page " > 尾页 </ asp:linkbutton >
< asp:label id = " Lab3 " runat = " server " ForeColor = " Blue " Height = " 7px " Font - Size = " 12px " ></ asp:label >
< asp:Label ID = " MessageLabel " ForeColor = " Blue " Text = " 页码: " runat = " server " />
< asp:DropDownList ID = " PageDropDownList " AutoPostBack = " true " runat = " server " OnSelectedIndexChanged = " PageDropDownList_SelectedIndexChanged " />& nbsp;
</ PagerTemplate >
后台:
protected
void
GridView1_DataBound(
object
sender, EventArgs e)
... {
try
...{
GridViewRow pagerRow = GridView1.BottomPagerRow;
LinkButton FirstPage = (LinkButton)pagerRow.Cells[0].FindControl("FirstPage");
LinkButton PreviousPage = (LinkButton)pagerRow.Cells[0].FindControl("PreviousPage");
LinkButton NextPage = (LinkButton)pagerRow.Cells[0].FindControl("NextPage");
LinkButton LastPage = (LinkButton)pagerRow.Cells[0].FindControl("LastPage");
if (GridView1.PageIndex == 0)
...{
FirstPage.Enabled = false;
PreviousPage.Enabled = false;
}
else if (GridView1.PageIndex == GridView1.PageCount - 1)
...{
LastPage.Enabled = false;
NextPage.Enabled = false;
}
else if (GridView1.PageCount <= 0)
...{
FirstPage.Enabled = false;
PreviousPage.Enabled = false;
NextPage.Enabled = false;
LastPage.Enabled = false;
}
DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
Label pageLabel = (Label)pagerRow.Cells[0].FindControl("CurrentPageLabel");
if (pageList != null)
...{
for (int i = 0; i < GridView1.PageCount; i++)
...{
int pageNumber = i + 1;
ListItem item = new ListItem(pageNumber.ToString() + "/" + GridView1.PageCount.ToString(), pageNumber.ToString());
if (i == GridView1.PageIndex)
...{
item.Selected = true;
}
pageList.Items.Add(item);
}
}
if (pageLabel != null)
...{
int currentPage = GridView1.PageIndex + 1;
pageLabel.Text = "当前页:" + currentPage.ToString() +
" / " + GridView1.PageCount.ToString();
}
catch ...{ }
}
... {
try
...{
GridViewRow pagerRow = GridView1.BottomPagerRow;
LinkButton FirstPage = (LinkButton)pagerRow.Cells[0].FindControl("FirstPage");
LinkButton PreviousPage = (LinkButton)pagerRow.Cells[0].FindControl("PreviousPage");
LinkButton NextPage = (LinkButton)pagerRow.Cells[0].FindControl("NextPage");
LinkButton LastPage = (LinkButton)pagerRow.Cells[0].FindControl("LastPage");
if (GridView1.PageIndex == 0)
...{
FirstPage.Enabled = false;
PreviousPage.Enabled = false;
}
else if (GridView1.PageIndex == GridView1.PageCount - 1)
...{
LastPage.Enabled = false;
NextPage.Enabled = false;
}
else if (GridView1.PageCount <= 0)
...{
FirstPage.Enabled = false;
PreviousPage.Enabled = false;
NextPage.Enabled = false;
LastPage.Enabled = false;
}
DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
Label pageLabel = (Label)pagerRow.Cells[0].FindControl("CurrentPageLabel");
if (pageList != null)
...{
for (int i = 0; i < GridView1.PageCount; i++)
...{
int pageNumber = i + 1;
ListItem item = new ListItem(pageNumber.ToString() + "/" + GridView1.PageCount.ToString(), pageNumber.ToString());
if (i == GridView1.PageIndex)
...{
item.Selected = true;
}
pageList.Items.Add(item);
}
}
if (pageLabel != null)
...{
int currentPage = GridView1.PageIndex + 1;
pageLabel.Text = "当前页:" + currentPage.ToString() +
" / " + GridView1.PageCount.ToString();
}
catch ...{ }
}