用下拉列表控制gridview的分页
aspx.cs中:
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
protected void PageDropDownList_SelectedIndexChanged(object sender,EventArgs e)
{
GridViewRow GVRow = GridView1.TopPagerRow;
DropDownList DDList = (DropDownList)GVRow.Cells[0].FindControl("PageDropDownList");
GridView1.PageIndex = DDList.SelectedIndex;
GridViewRow pagerRow = GridView1.BottomPagerRow;
DropDownList DDList1 = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
GridView1.PageIndex = DDList1.SelectedIndex;
GridView1.DataBind();
}
protected void GridView1_DataBound(object sender, EventArgs e)
{
try
{
GridViewRow pagerRow1 = GridView1.BottomPagerRow;
GridViewRow pagerRow = GridView1.TopPagerRow;
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 (GridView1.PageIndex == 0)
{
linkBtnFirst.Enabled = false;
linkBtnPrev.Enabled = false;
}
else if (GridView1.PageIndex == GridView1.PageCount - 1)
{
linkBtnLast.Enabled = false;
linkBtnNext.Enabled = false;
}
else if (GridView1.PageCount <= 0)
{
linkBtnFirst.Enabled = false;
linkBtnPrev.Enabled = false;
linkBtnNext.Enabled = false;
linkBtnLast.Enabled = false;
}
LinkButton linkBtnFirst1 = (LinkButton)pagerRow1.Cells[0].FindControl("linkBtnFirst");
LinkButton linkBtnPrev1 = (LinkButton)pagerRow1.Cells[0].FindControl("linkBtnPrev");
LinkButton linkBtnNext1 = (LinkButton)pagerRow1.Cells[0].FindControl("linkBtnNext");
LinkButton linkBtnLast1 = (LinkButton)pagerRow1.Cells[0].FindControl("linkBtnLast");
if (GridView1.PageIndex == 0)
{
linkBtnFirst1.Enabled = false;
linkBtnPrev1.Enabled = false;
}
else if (GridView1.PageIndex == GridView1.PageCount - 1)
{
linkBtnLast1.Enabled = false;
linkBtnNext1.Enabled = false;
}
else if (GridView1.PageCount <= 0)
{
linkBtnFirst1.Enabled = false;
linkBtnPrev1.Enabled = false;
linkBtnNext1.Enabled = false;
linkBtnLast1.Enabled = false;
}
DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
Label pageLabel = (Label)pagerRow.Cells[0].FindControl("CurrentPageLabel");
DropDownList pageList1 = (DropDownList)pagerRow1.Cells[0].FindControl("PageDropDownList");
Label pageLabel1 = (Label)pagerRow1.Cells[0].FindControl("CurrentPageLabel");
if (pageList != null && pageList1!=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);
pageList1.Items.Add(item);
}
}
if (pageLabel != null)
{
int currentPage = GridView1.PageIndex + 1;
pageLabel.Text = pageLabel1.Text = "当前页: " + currentPage.ToString() +
" / " + GridView1.PageCount.ToString();
}
}
catch
{
Label1.Text = "尚无数据。";
}
}
aspx中:
<div style="overflow-x: auto;overflow-y: auto;height: 300px;width:1000px">
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="15" Width="207px" OnPageIndexChanging="GridView1_PageIndexChanging" OnDataBound="GridView1_DataBound" CellPadding="0" EnableViewState="False" UseAccessibleHeader="False">
<PagerSettings Position="TopAndBottom" />
<PagerTemplate>
<table width="900">
<tr height="20">
<td width="70%" align="left">
<asp:Label ID="CurrentPageLabel" runat="server"></asp:Label>
</td>
<td width="70%">
<asp:LinkButton CommandName="Page" CommandArgument="First" ID="linkBtnFirst" runat="server">首页</asp:LinkButton>
<asp:LinkButton CommandName="Page" CommandArgument="Prev" ID="linkBtnPrev" runat="server">上一页</asp:LinkButton>
<asp:LinkButton CommandName="Page" CommandArgument="Next" ID="linkBtnNext" runat="server">下一页</asp:LinkButton>
<asp:LinkButton CommandName="Page" CommandArgument="Last" ID="linkBtnLast" runat="server">末页</asp:LinkButton>
<asp:Label ID="MessageLabel" ForeColor="Blue" Text="页码:" runat="server" />
<asp:DropDownList ID="PageDropDownList" AutoPostBack="true" OnSelectedIndexChanged="PageDropDownList_SelectedIndexChanged" runat="server" />
</td>
</tr>
</table>
</PagerTemplate>
</asp:GridView>
</div>