A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

转自:http://www.jb51.net/article/49848.htm
这篇文章主要介绍了c#分页显示服务器上指定目录下的所有图片示例,需要的朋友可以参考下
c#分页显示服务器上指定目录下的所有图片

  1. <%@ Page Language="C#" EnableViewState="false" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <script runat="server">
  5. String folder = "~/";// 网站根目录下的所有图像
  6. protected void Page_Load(object sender, EventArgs e)
  7. {
  8. //总页数
  9. int TotalPages = 0;
  10. //每页显示的数量
  11. int pageItem = 4;
  12. //当前页号
  13. int pageIndex = Convert.ToInt32(Request.QueryString["page"]);
  14. if (pageIndex == 0) pageIndex = 1;
  15. System.IO.DirectoryInfo d = new System.IO.DirectoryInfo(Server.MapPath(folder));
  16. System.IO.FileInfo[] fs = d.GetFiles("*.*").Where(file => file.Name.ToLower().EndsWith(".jpg") || file.Name.ToLower().EndsWith(".gif") || file.Name.ToLower().EndsWith(".bmp") || file.Name.ToLower().EndsWith(".png")).ToArray();
  17. if (fs.Length % pageItem == 0) TotalPages = fs.Length / pageItem;
  18. else
  19. {
  20. TotalPages = (int)Math.Ceiling((decimal)fs.Length / (decimal)pageItem);
  21. }
  22. if (pageIndex > TotalPages) pageIndex = TotalPages;
  23. System.IO.FileInfo[] fs2 = new System.IO.FileInfo[pageItem];
  24. int leftCount = pageItem;
  25. if (fs.Length - ((pageIndex - 1) * pageItem) < pageItem)
  26. {
  27. leftCount = fs.Length - (pageIndex - 1) * pageItem;
  28. }
  29. Array.Copy(fs, (pageIndex - 1) * pageItem, fs2, 0, leftCount);
  30. DataList1.DataSource = fs2;
  31. DataList1.DataBind();
  32. PrePage.NavigateUrl = Request.FilePath + "?page=" + (pageIndex - 1);
  33. NextPage.NavigateUrl = Request.FilePath + "?page=" + (pageIndex + 1);
  34. Label1.Text = fs.Length + "张图片 共" + TotalPages + "页 第" + pageIndex + "页";
  35. }
  36. </script>
  37. <html xmlns="http://www.w3.org/1999/xhtml">
  38. <body>
  39. <form runat="server">
  40. <asp:DataList ID="DataList1" runat="server" RepeatColumns="2" Border="1">
  41. <ItemTemplate>
  42. <img src='<%#Page.ResolveUrl(folder + Eval("Name"))%>' width="200" height="200" />
  43. </ItemTemplate>
  44. <AlternatingItemTemplate>
  45. <img src='<%#Page.ResolveUrl(folder + Eval("Name"))%>' width="200" height="200" />
  46. </AlternatingItemTemplate>
  47. </asp:DataList>
  48. <asp:HyperLink ID="PrePage" runat="server">上一页</asp:HyperLink>
  49. <asp:HyperLink ID="NextPage" runat="server">下一页</asp:HyperLink>
  50. <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
  51. </form>
  52. </body>
  53. </html>
复制代码


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马