首页 > 编程 > .NET > 正文

asp.net 通过aspnetpager为DataList分页

2024-07-10 13:18:15
字体:
来源:转载
供稿:网友
前台代码:
代码

复制代码 代码如下:


<div>
<asp:DataList runat="server">
<HeaderTemplate>
<div>
<img src="../loginimages/newslisthead.gif" /></div>
</HeaderTemplate>
<ItemTemplate>
<dl>
<dd>
·<a href="#"><%#Eval("NewsTitle") %></a>
   <%#Eval("NewsTime", "{0:yyyy-MM-dd}")%><br />
</dd>
</dl>
</ItemTemplate>
</asp:DataList>
</div>
<div>
<webdiyer:AspNetPager runat="server" PageSize="15" FirstPageText=" 首页 "
LastPageText=" 尾页 " NextPageText=" 下一页 " OnPageChanged="AspNetPager1_PageChanged"
PrevPageText=" 上一页 " 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();
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表