protected void DetailsView1_ItemCommand(object sender, DetailsViewCommandEventArgs e)
{
if (e.CommandName == "up")
{
FileUpload fup = DetailsView1.FindControl("FileUpload1") as FileUpload;
string dirPath = "admin/images/UpFile/";
string dirPath2 = "images/UpFile/";
string strRs = Common.UpFile("~/" + dirPath, fup, Label2);
if (strRs != null)
{
((HiddenField)DetailsView1.FindControl("hidImage")).Value = dirPath2 + strRs;
((Image)DetailsView1.FindControl("Image1")).ImageUrl = "~/" + dirPath + strRs;
}
}
}
这个是赋值的
-----------------------
这个是图片上传
public static string UpFile(string dirPath, FileUpload FileUpload1, Label Label1)
{
if (!Directory.Exists(HttpContext.Current.Server.MapPath(dirPath)))
{
Directory.CreateDirectory(HttpContext.Current.Server.MapPath(dirPath));
}
if (FileUpload1.PostedFile.ContentType == "image/jpg" || FileUpload1.PostedFile.ContentType == "image/gif" || FileUpload1.PostedFile.ContentType == "image/jpeg" || FileUpload1.PostedFile.ContentType == "image/pjpeg")
{
if (FileUpload1.PostedFile.ContentLength > 500 * 1024)
{
Label1.Text = Common.Alert("图片大小不正确!");
return null;
}
else
{
string pic = Guid.NewGuid().ToString();
string pictype = FileUpload1.FileName.Substring(FileUpload1.FileName.LastIndexOf("."));
string picname = pic + pictype;
string desc = HttpContext.Current.Server.MapPath(dirPath);
FileUpload1.SaveAs(desc + picname);
return picname;
}
}
else
{
Label1.Text = Common.Alert("图片类型不正确!");
return null;
}
}
具体路径什么的自己修改
给按钮写点击事件
动态输出img
buzhidao