我正在尝试使用转发器显示文件夹图像中的图片,但无法显示问号图片我不知道原因。
<div data-u="slides" style="cursor: default; position: relative; top: 0px; left: 0px; width: 800px; height: 356px; overflow: hidden;">
<asp:Repeater runat="server" ID="RepaterImages">
<ItemTemplate>
<div runat="server" data-p="144.50">
<img id="Image" runat="server" data-u="image" style="Width:120px;" src='<%#Container.DataItem %>'/>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
protected void Page_Load(object sender, EventArgs e)
{
int id = Convert.ToInt32(Request.QueryString["id"]);
var path = Server.MapPath("ProjectsImages/ ");
var images = Directory.GetFiles(path,id+"*");
ArrayList list = new ArrayList();
foreach (var img in images)
{
list.Add(img);
}
RepaterImages.DataSource = images;
RepaterImages.DataBind();
}
答案 0 :(得分:3)
您的代码存在的问题是,您正在使用图像的物理路径。您必须提供转发器的相对路径。如下所示更改您的代码,它应该工作我认为:
protected void Page_Load(object sender, EventArgs e)
{
try
{
int id = Convert.ToInt32(Request.QueryString["id"]);
string relativePath = "/ProjectsImages/";
var path = Server.MapPath(relativePath);
var images = Directory.GetFiles(path, id + "*").Select(x =>
{
var arrPath = x.Split('\\');
string imgName = arrPath[arrPath.Length - 1];
return relativePath + imgName;
});
RepaterImages.DataSource = images;
RepaterImages.DataBind();
}
catch (Exception ex)
{
throw ex;
}
}
答案 1 :(得分:0)
您错过了Postback属性。请使用以下代码。
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
int id = Convert.ToInt32(Request.QueryString["id"]);
var path = Server.MapPath("ProjectsImages/ ");
var images = Directory.GetFiles(path,id+"*");
ArrayList list = new ArrayList();
foreach (var img in images)
{
list.Add(img);
}
RepaterImages.DataSource = list;
RepaterImages.DataBind();
}
}