如何使用 Eval 从不同的文件夹将图像绑定到图像控件?



我有如下代码:

<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl='<%# "../Image/Products/1/" + Eval("P_Pic")   %>' />

这是用于从文件夹读取图像地址 .imageButton 控件位于列表视图中,并且从多个表中检索数据,图像驻留在不同的文件夹中...它的意思是这部分代码:"../图像/产品/1/"每个表应该不同。你能帮我处理这个问题吗?谢谢 注意:每个表用于不同的产品,此代码是因为合并所有产品以显示

我找到了一种方法来处理这个问题

public string GetImage(object P_Pic )
{
if (P_Pic != null & ProductCategory == "a")
{
string Pic = "../Image/Products/a/" + P_Pic;
//do something with the ImageID to return the image path as string
return Pic;
}
else if (P_Pic != null & ProductCategory == "b")
{
string Pic = "../Image/Products/b/" + P_Pic;
return Pic;
}
else if (P_Pic != null & ProductCategory == "c")
{
string Pic = "../Image/Products/c/" + P_Pic;
return Pic;
}
else if (P_Pic != null & ProductCategory == "d")
{
string Pic = "../Image/Products/d/" + P_Pic;
return Pic;
}
else if (P_Pic != null & ProductCategory == "e")
{
string Pic = "../Image/Products/e/" + P_Pic;
return Pic;
}
else if (P_Pic != null & ProductCategory == "f")
{
string Pic = "../Image/Products/f/" + P_Pic;
return Pic;
}
else
{
return "";
}
}

其中"产品类别"是传递到此处的会话。下一个在控件中,我添加了以下代码:

<img id="img" class="img" runat="server" src='<%# GetImage(Eval("P_Pic")) %>' />

这就是全部:)

最新更新