欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net 通过aspnetpager为DataList分页

程序员文章站 2024-03-08 15:06:34
前台代码: 代码 复制代码 代码如下:
前台代码:
代码
复制代码 代码如下:

<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();
}