微软的Datalist在做电子相册时候必不可少,但是不支持分页功能,都出到2008了还是没有分页功能,幸好网上有个专业的分页空间aspnetpager,帮我们解决了大问题,说实在话的,网上关于datalist分页的办法很多,但是我觉得都不是很简单实用,有的是url提交,有的是存储过程分页,一个字不方便。下面就是aspnetpager的分页,代码。
1.首先下载aspnetpager控件,http://www.webdiyer.com/download/default.aspx。
2.在工具箱里面添加aspnetpager控件。
3.前台引用
<asp:DataList runat="server" ID="dlPicList" Width="100%" Border="0" CellSpacing="0" CellPadding="0" RepeatColumns="4" RepeatDirection="Horizontal">
<ItemTemplate>
<a href='<%# "PicView.aspx?PicID="+Eval("PicID")%>'><asp:Image ID="imPic" runat="server" ImageUrl='<%# Bind("PicUrl")%>' width="104" height="76" BorderWidth="1" /><br />
<asp:Label runat="server" ID="lblTitle" Text='<%# Bind("DisTitle")%>'></asp:Label></a>
</ItemTemplate>
<ItemStyle CssClass="word_green" HorizontalAlign="Center" />
</asp:DataList>
<webdiyer:AspNetPager ID="AspNetPager1" runat="server" PageSize="5" ShowBoxThreshold="1" AlwaysShow="True" FirstPageText="首页" LastPageText="末页"
NextPageText="下一页" PrevPageText="上一页" onpagechanging="AspNetPager1_PageChanging">
</webdiyer:AspNetPager>
4.后台代码
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
private void BindData()
{
conn = ConfigurationManager.ConnectionStrings["STRING_SqlConn"].ToString();
object[] para = { 0, picid };
DataTable dt = DataOperator.TableUseTrans(conn, "SP_Pic_GetView", para);
DataView dv = dt.DefaultView;
//对用于分页的类的引用
PagedDataSource pds = new PagedDataSource();
pds.DataSource = dv;//设置数据源(DataTable类型)
pds.AllowPaging = true;
//每页显示的行数
//pds.PageSize = 12;
AspNetPager1.RecordCount = dv.Count;
pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
pds.PageSize = AspNetPager1.PageSize;
dlPicList.DataSource= pds;
dlPicList.DataBind();
}
protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
{
AspNetPager1.CurrentPageIndex = e.NewPageIndex;
BindData();
}