实现如上图所示的分页,具体代码备忘:
前台:
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="REGIONCODE" AllowPaging="True"
onrowdatabound="GridView1_RowDataBound" ondatabound="GridView1_DataBound"
onpageindexchanging="GridView1_PageIndexChanging1" AllowSorting="True"
onsorting="GridView1_Sorting" PageSize="15">
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="ID"
DataNavigateUrlFormatString="~/notice/noticechakan.aspx?ID={0}"
DataTextField="TITLE" HeaderText="通知标题" Target="_blank" Text="{0}" >
<HeaderStyle HorizontalAlign="Center" Width="80%" />
<ItemStyle HorizontalAlign="left" />
</asp:HyperLinkField>
<asp:BoundField DataField="SENDTIME" HeaderText="发送日期"
HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" DataFormatString="{0:yyyy-MM-dd}" HtmlEncode="False">
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="REGIONCODE" HeaderText="用户"
HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundField>
</Columns>
<PagerTemplate>
<table width="100%">
<tr>
<td width="70%">
<asp:Label ID="lb_articlenum" runat="server" />
<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>
<td width="30%" align="right">
<asp:Label ID="CurrentPageLabel" ForeColor="Blue" runat="server" />
</td>
</tr>
</table>
</PagerTemplate>
</asp:GridView>
<asp:Label ID="lb_sql" runat="server" Visible="False"></asp:Label>
<asp:Label ID="lb_count" runat="server" Visible="False"></asp:Label>
后台代码:
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
public void gridview_databind(string sql)
{
BLLibrary.Bbase_notice noticedal = new BLLibrary.Bbase_notice();
IList<Model.base_noticeEntity> noticeEntitys = noticedal.Getbase_noticesbyCondition(sql+"order by sendtime desc");
lb_count.Text = noticeEntitys.Count.ToString();
GridView1.DataSource = noticeEntitys;
GridView1.DataBind();
}//gridview的数据绑定
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
//重新绑定数据
gridview_databind(lb_sql.Text);
}
protected void GridView1_DataBound(object sender, EventArgs e)
{
GridViewRow pagerRow = GridView1.BottomPagerRow;
if (pagerRow != null)
{
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;
}
DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
Label pageLabel = (Label)pagerRow.Cells[0].FindControl("CurrentPageLabel");
Label articlenum = (Label)pagerRow.Cells[0].FindControl("lb_articlenum");
articlenum.Text = "共有" + lb_count.Text + "条数据,本页" + GridView1.Rows.Count.ToString() + "条数据 ";
if (pageList != null)
{
for (int i = 0; i < GridView1.PageCount; i++)
{
int pageNumber = i + 1;
ListItem item = new ListItem("第" + pageNumber.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();
}
}
}
protected void PageDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow pagerRow = GridView1.BottomPagerRow;
DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
GridView1.PageIndex = pageList.SelectedIndex;
gridview_databind(lb_sql.Text);
}
这个是在做一个项目时用到的,自己留下来备忘。