<script runat="server">
void PageDropDownList_SelectedIndexChanged(Object sender, EventArgs e)
{
GridViewRow pagerRow = CustomersGridView.BottomPagerRow;
DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
CustomersGridView.PageIndex = pageList.SelectedIndex;
}
void CustomersGridView_DataBound(Object sender, EventArgs e)
{
GridViewRow pagerRow = CustomersGridView.BottomPagerRow;
LinkButton linkBtnFirst = (LinkButton)pagerRow.Cells[0].FindControl("linkBtnFirst");
LinkButton linkBtnPrev = (LinkButton)pagerRow.Cells[0].FindControl("linkBtnPrev");
LinkButton linkBtnNext = (LinkButton)pagerRow.Cells[0].FindControl("linkBtnNext");
LinkButton linkBtnLast = (LinkButton)pagerRow.Cells[0].FindControl("linkBtnLast");
if (CustomersGridView.PageIndex == 0)
{
linkBtnFirst.Enabled = false;
linkBtnPrev.Enabled = false;
}
else if (CustomersGridView.PageIndex == CustomersGridView.PageCount-1)
{
linkBtnLast.Enabled = false;
linkBtnNext.Enabled = false;
}
else if (CustomersGridView.PageCount<=0)
{
linkBtnFirst.Enabled = false;
linkBtnPrev.Enabled = false;
linkBtnNext.Enabled = false;
linkBtnLast.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 < CustomersGridView.PageCount; i++)
{
int pageNumber = i + 1;
ListItem item = new ListItem(pageNumber.ToString() + "https://www.jb51.net/" + CustomersGridView.PageCount.ToString(), pageNumber.ToString());
if (i == CustomersGridView.PageIndex)
{
item.Selected = true;
}
pageList.Items.Add(item);
}
}
if (pageLabel != null)
{
int currentPage = CustomersGridView.PageIndex + 1;
pageLabel.Text = "当前页: " + currentPage.ToString() +
" / " + CustomersGridView.PageCount.ToString();
}
}
</script>
<html>
<body>
<form runat="server">
<h3>
GridView PagerTemplate Example</h3>
<asp:WebGridView DataSourceID="CustomersSqlDataSource" AutoGenerateColumns="true"
AllowPaging="true" OnDataBound="CustomersGridView_DataBound" SortAscImageUrl="~\images\arrow-up.gif" SortDescImageUrl="~\images\arrow-down.gif" runat="server" AllowSorting="True">
<PagerStyle ForeColor="Blue" BackColor="LightBlue" />
<PagerTemplate>
<table>
<tr>
<td>
<asp:Label ForeColor="Blue" Text="页码:" runat="server" />
<asp:DropDownList AutoPostBack="true" OnSelectedIndexChanged="PageDropDownList_SelectedIndexChanged"
runat="server" />
<asp:LinkButton CommandName="Page" CommandArgument="First" runat="server">首页</asp:LinkButton>
<asp:LinkButton CommandName="Page" CommandArgument="Prev" runat="server">上一页</asp:LinkButton>
<asp:LinkButton CommandName="Page" CommandArgument="Next" runat="server">下一页</asp:LinkButton>
<asp:LinkButton CommandName="Page" CommandArgument="Last" runat="server">末页</asp:LinkButton>
</td>
<td>
<asp:Label ForeColor="Blue" runat="server" />
</td>
</tr>
</table>
</PagerTemplate>
</asp:WebGridView>
<asp:SqlDataSource SelectCommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
ConnectionString="<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server">
</asp:SqlDataSource>
</form>
</body>
</html>
您可能感兴趣的文章: