今天搞CMS的时候又遇到一个问题:由于新闻列表页面用的是GridView,觉得自带的分页不怎么样,就加入了DropDownList,感觉会人性化一些,但是,也出了问题!
艰难的为DropDownList动态绑定了值,但是,Change事件却不能翻页,而且不能保存状态,于是,百度,Google搜罗了一箩筐,问题还没解决,看来我真的很笨,这个问题都这么拗口.
先贴出部分源码,以后解决了再贴上解决方法.
页面部分:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
<PagerTemplate>
<asp:LinkButton ID="FirstPage" runat="server" CommandArgument="First" ForeColor="AntiqueWhite" CommandName="Page" Enabled=" <%# ((GridView)Container.NamingContainer).PageIndex != 0%>">首页</asp:LinkButton>
<asp:LinkButton ID="PreviousPage" runat="server" CommandArgument="Prev" ForeColor="AntiqueWhite" CommandName="Page" Enabled=" <%# ((GridView)Container.NamingContainer).PageIndex != 0 %>">上一页</asp:LinkButton>
<asp:LinkButton ID="NextPage" runat="server" CommandArgument="Next" ForeColor="AntiqueWhite" CommandName="Page" Enabled=" <%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>">下一页</asp:LinkButton>
<asp:LinkButton ID="LastPage" runat="server" CommandArgument="Last" ForeColor="AntiqueWhite" CommandName="Page" Enabled=" <%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>">尾页</asp:LinkButton>
共 <font color="red"><%=RecordCount%></font> 条记录 当前第 <font color="red"><%#((GridView)Container.NamingContainer).PageIndex+1%></font> 页/共 <font color="red"><%#((GridView)Container.NamingContainer).PageCount %></font> 页 每页 <font color="red"><%#((GridView)Container.NamingContainer).PageSize %></font> 条记录 转到:
<asp:DropDownList runat="server" ID="ChangePage" AutoPostBack="true" OnSelectedIndexChanged="ChangePage_SelectedIndexChanged">
</asp:DropDownList>
</PagerTemplate>CS:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
protected void GridView1_DataBound(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (this.GridView1.Rows.Count > 0)
{
GridViewRow pagerRow = this.GridView1.BottomPagerRow;
DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("ChangePage");
for (int i = 1; i <= this.GridView1.PageCount; i++)
{
pageList.Items.Add(new ListItem("第" + i.ToString() + "页", i.ToString()));
//if ((this.GridView1.PageIndex + 1) == i)
// pageList.SelectedIndex = i;
}
pageList.SelectedValue = Convert.ToString(this.GridView1.PageIndex);
}
}
}DropDownList事件:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
protected void ChangePage_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow pagerRow = this.GridView1.BottomPagerRow;
DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("ChangePage");
Session["NewsPage"] = pageList.SelectedValue;
BindNewsWithClassID();
}
1 protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
2 {
3 this.GridView1.PageIndex = e.NewPageIndex;
4 BindNewsWithClassID();
5 }