asp.net 通过aspnetpager为DataList分页
作者:
今天整了半天才把DataList的分页搞定,下面把我的设计过程给大家讲讲
前台代码:
代码
<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();
}
代码
复制代码 代码如下:
<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();
}
您可能感兴趣的文章:
- asp.net中让Repeater和GridView支持DataPager分页
- Asp.Net数据控件引用AspNetPager.dll分页实现代码
- 分享一个asp.net pager分页控件
- asp.net分页控件AspNetPager的样式美化
- Asp.Net分页和AspNetPager控件的使用
- asp.net下Repeater使用 AspNetPager分页控件
- asp.net结合aspnetpager使用SQL2005的存储过程分页
- AspNetAjaxPager,Asp.Net通用无刷新Ajax分页控件,支持多样式多数据绑定
- asp.net 分页sql语句(结合aspnetpager)
- asp.net mvc自定义pager封装与优化