aspx页面代码
<asp:DataList ID="dlPhoto" runat="server" Height="137px" Width="277px" onitemcommand="dlPhoto_ItemCommand" RepeatDirection="Horizontal"> <ItemTemplate> <%# DataBinder.Eval(Container.DataItem, "imageUrl") %> <asp:ImageButton ID="ImageButton1" ImageUrl="image/20131128.jpg" Height="100px" Width ="200px" runat="server" /><br /> <asp:Label ID="Label1" runat="server" >相册名称</asp:Label> <asp:ImageButton id="ProductImage" ImageUrl='<%# DataBinder.Eval(Container.DataItem, "imageUrl") %>' Height="100px" Width ="200px" runat="server"/> </ItemTemplate> <FooterTemplate> <asp:LinkButton ID="LinkButton1" CommandName="first" runat="server" >首页</asp:LinkButton> <asp:LinkButton ID="LinkButton2" CommandName="pre" runat="server">上一页</asp:LinkButton> <asp:LinkButton ID="LinkButton3" CommandName="next" runat="server">下一页</asp:LinkButton> <asp:LinkButton ID="LinkButton4" CommandName="last" runat="server">末页</asp:LinkButton> <asp:TextBox ID="txtPage" runat="server" Height="18px" Width="34px"></asp:TextBox> <asp:LinkButton ID="LinkButton5" CommandName="search" runat="server">Go</asp:LinkButton> </FooterTemplate> </asp:DataList>
aspx.cs
public partial class _232DataList : System.Web.UI.Page { PagedDataSource pds = new PagedDataSource(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { bindDL(0); } } public void bindDL(int curPage) { pds.AllowPaging = true; pds.PageSize = 2; pds.CurrentPageIndex = curPage; //绑定数据源 ShowImageBll ShowImageBll = new BLL.ShowImageBll(); DataSet ds=ShowImageBll.GetList(); pds.DataSource = ds.Tables[0].DefaultView; dlPhoto.DataSource = pds; dlPhoto.DataBind(); } protected void dlPhoto_ItemCommand(object source, DataListCommandEventArgs e) { switch (e.CommandName) { case "first": if (pds.CurrentPageIndex != 0) { pds.CurrentPageIndex = 0; } else { Response.Write("<script language=javascript>" + "alert(\"已经是第一页\")" + "</script>"); } bindDL(pds.CurrentPageIndex); break; case "pre": if (pds.CurrentPageIndex > 0) { pds.CurrentPageIndex = pds.CurrentPageIndex - 1; bindDL(pds.CurrentPageIndex); } else { Response.Write("<script language=javascript>" + "alert(\"已经是第一页\")" + "</script>"); } break; case "next": if (pds.CurrentPageIndex < pds.PageCount - 1) { pds.CurrentPageIndex = pds.CurrentPageIndex + 1; bindDL(pds.CurrentPageIndex); } else { Response.Write("<script language=javascript>" + "alert(\"已经是最后一页\")" + "</script>"); } break; case "last": if (pds.CurrentPageIndex != pds.PageCount - 1) { pds.CurrentPageIndex = pds.PageCount - 1; } else { Response.Write("<script language=javascript>" + "alert(\"已经是最后一页\")" + "</script>"); } bindDL(pds.CurrentPageIndex); break; case "search": if (e.Item.ItemType == ListItemType.Footer) { int pageCount = int.Parse(pds.PageCount.ToString ()); TextBox txtPage = e.Item.FindControl("txtPage") as TextBox; int myPage = 0; if(txtPage .Text !=null) { myPage = int.Parse(txtPage .Text.Trim ().ToString ()); } if (myPage <=0||myPage >pageCount ) { Response .Write ("<script>alert('请输入正确的页面数!')</script>"); } bindDL(myPage-1); } break; } } }