首页 > 代码库 > 根据条件查看图片DataList
根据条件查看图片DataList
最近做项目遇到把图片式存入到服务器,然后再读取到DataList中
页面:
<div style=" margin-top:20px">
<asp:DataList ID="DataList1" runat="server" RepeatDirection="Horizontal" RepeatColumns="3">
<ItemTemplate>
<div style="width:600px">
<img src=http://www.mamicode.com/‘<%#Eval("FilePath") %>‘ width="200" height="200"/>
<br />
<%# Eval("FileName") %>
</div>
</ItemTemplate>
</asp:DataList>
</div>
如图:
后台:
DataTable dt = new DataTable();
dt.Columns.Add("FileName");
dt.Columns.Add("FilePath");//显示
string imageUrl = "/UserData/uploadimg/";//应用程序路径
string imgPath = Server.MapPath(imageUrl);//获得的路径都是服务器上的物理路径,也就是常说的绝对路径
if (string.IsNullOrEmpty(this.ChoseDate.Text))
{
Response.Write("<script>alert(‘请选择日期‘)</script>");
return;
}
DateTime ChoseDate = DateTime.Parse(this.ChoseDate.Text);//时间范围
//if (string.IsNullOrEmpty(this.txtMachine.Text))
//{
// Response.Write("<script>alert(‘请输入机器代码‘)</script>");
// return;
//}
string machineCode = this.txtMachine.Text.Trim();//机器代码
string ImageDire = ChoseDate.ToString("yyyyMMdd");//获取时间文件夹为20140603
//找到满足条件的文件夹
string SearchFileName = machineCode + "_" + ImageDire;//机器代码+20140202
//路径
string ImagePathRoot = ConfigurationManager.AppSettings["ImagePathRoot"];//UserData/uploadimg
string MachineImagePathRoot = ConfigurationManager.AppSettings["MachineImagePath"];//C:\MyProjects\SU8PAD\src\HotelPadSolution\HotelPadWeb\UserData\uploadimg\
//返回文件列表
string FullFilePath = MachineImagePathRoot + ImageDire;
if (!Directory.Exists(FullFilePath))//没有文件进来
{
Response.Write("<script>alert(‘文件不存在‘)</script>");
return;
}
string[] PathList = Directory.GetFiles(FullFilePath);//静态
//找图片
for (int i = 0; i < PathList.Length; i++)
{
PathList[i] = PathList[i].Replace(@"\","/");
string FileName = PathList[i].Substring(PathList[i].LastIndexOf(‘/‘)+1);//文件名
//截取文件的日期
string TempFIleNameCode = FileName.Substring(FileName.IndexOf(‘_‘)+1);
string FIleNameDate = TempFIleNameCode.Substring(0,TempFIleNameCode.LastIndexOf(‘.‘));
FIleNameDate = FIleNameDate.Substring(0,8);
FIleNameDate=FIleNameDate.Insert(4, "-");
FIleNameDate=FIleNameDate.Insert(7, "-");
DateTime CurrentFileDate = DateTime.Parse(FIleNameDate);
string TempMachineCode = FileName.Substring(0,FileName.IndexOf(‘_‘));//机器代码
//取日期差
TimeSpan ts = new TimeSpan();
ts = ChoseDate - CurrentFileDate;
if (ts.TotalDays == 0 && (FileName.Contains(machineCode) || machineCode == ""))//模糊
{
DataRow dr = dt.NewRow();
dr[0] = FileName;
dr[1] = ImagePathRoot + ImageDire+"/" + FileName;//PathList[i];
dt.Rows.InsertAt(dr, dt.Rows.Count);
}
}
this.DataList1.DataSource = dt;
this.DataList1.DataBind();
}
根据条件查看图片DataList