前台代碼:
代碼
復制代碼 代碼如下:
<div id="newslistdiv">
<asp:DataList ID="NewsList" runat="server" Width="820px">
<HeaderTemplate>
<div>
<img src="../loginimages/newslisthead.gif" alt="" /></div>
</HeaderTemplate>
<ItemTemplate>
<dl>
<dd>
·<a href="#" class="rlk" onclick='window.location.href="DetailNews.aspx?ID=<%#Eval("NewsID") %>"'><%#Eval("NewsTitle") %></a>
<%#Eval("NewsTime", "{0:yyyy-MM-dd}")%><br />
</dd>
</dl>
</ItemTemplate>
</asp:DataList>
</div>
<div style="margin-bottom: 2px;" align="center">
<webdiyer:AspNetPager ID="AspNetPager1" runat="server" PageSize="15" FirstPageText=" 首頁 "
LastPageText=" 尾頁 " NextPageText=" 下一頁 " OnPageChanged="AspNetPager1_PageChanged"
PrevPageText=" 上一頁 " Width="100%" AlwaysShow="true" CssClass="anpager">
</webdiyer:AspNetPager>
</div>
後台代碼:
代碼
復制代碼 代碼如下:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindNewsDatalist();
}
}
//綁定DataList的數據源
private void BindNewsDatalist()
{
FJDWS.BusinessLogic.LNewsManager lnewsmanager = new FJDWS.BusinessLogic.LNewsManager();
this.AspNetPager1.RecordCount = lnewsmanager.FindAll().Count;
PagedDataSource pds = new PagedDataSource(); //定義一個PagedDataSource類來執行分頁功能
pds.DataSource = lnewsmanager.FindAll();
pds.AllowPaging = true;
pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
pds.PageSize = AspNetPager1.PageSize;
this.NewsList.DataSource = pds;
NewsList.DataBind();
}
protected void AspNetPager1_PageChanged(object src, EventArgs e)
{
//AspNetPager1.CurrentPageIndex = e.NewPageIndex;
BindNewsDatalist();
}