c#分页显示图片 c#分页显示服务器上指定目录下的全部图片示例
人气:0c#分页显示服务器上指定目录下的所有图片
<%@ Page Language="C#" EnableViewState="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
String folder = "~/";// 网站根目录下的所有图像
protected void Page_Load(object sender, EventArgs e)
{
//总页数
int TotalPages = 0;
//每页显示的数量
int pageItem = 4;
//当前页号
int pageIndex = Convert.ToInt32(Request.QueryString["page"]);
if (pageIndex == 0) pageIndex = 1;
System.IO.DirectoryInfo d = new System.IO.DirectoryInfo(Server.MapPath(folder));
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();
if (fs.Length % pageItem == 0) TotalPages = fs.Length / pageItem;
else
{
TotalPages = (int)Math.Ceiling((decimal)fs.Length / (decimal)pageItem);
}
if (pageIndex > TotalPages) pageIndex = TotalPages;
System.IO.FileInfo[] fs2 = new System.IO.FileInfo[pageItem];
int leftCount = pageItem;
if (fs.Length - ((pageIndex - 1) * pageItem) < pageItem)
{
leftCount = fs.Length - (pageIndex - 1) * pageItem;
}
Array.Copy(fs, (pageIndex - 1) * pageItem, fs2, 0, leftCount);
DataList1.DataSource = fs2;
DataList1.DataBind();
PrePage.NavigateUrl = Request.FilePath + "?page=" + (pageIndex - 1);
NextPage.NavigateUrl = Request.FilePath + "?page=" + (pageIndex + 1);
Label1.Text = fs.Length + "张图片 共" + TotalPages + "页 第" + pageIndex + "页";
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<form runat="server">
<asp:DataList ID="DataList1" runat="server" RepeatColumns="2" Border="1">
<ItemTemplate>
<img src='<%#Page.ResolveUrl(folder + Eval("Name"))%>' width="200" height="200" />
</ItemTemplate>
<AlternatingItemTemplate>
<img src='<%#Page.ResolveUrl(folder + Eval("Name"))%>' width="200" height="200" />
</AlternatingItemTemplate>
</asp:DataList>
<asp:HyperLink ID="PrePage" runat="server">上一页</asp:HyperLink>
<asp:HyperLink ID="NextPage" runat="server">下一页</asp:HyperLink>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</form>
</body>
</html>
加载全部内容