c#分页显示服务器上指定目录下的所有图片示例
c#分页显示服务器上指定目录下的所有图片
<%@ 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>
上一篇: c#扩展datatable转json示例
下一篇: c#线程Thread示例