【问题标题】:how to binding images to image control with Eval from defferent folders?如何使用来自不同文件夹的 Eval 将图像绑定到图像控件?
【发布时间】:2017-08-12 13:30:43
【问题描述】:

我有如下代码:

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

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

【问题讨论】:

  • 这个问题很难理解。为什么不将图像路径存储在数据库中?
  • 例如,我有 6 个产品类别,网站菜单的访问者选择了一个类别,转到相关页面,然后...存在另一个选项,访问者点击显示所有产品。现在开始我保存每个产品图像都在单独的文件夹中,所以当我想显示所有产品时,只会遇到一个问题,不同的文件夹以及如何使用 Eval 等运行时方法解决控制问题。
  • 使用一个文件夹存放所有图片?

标签: asp.net eval 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 "";
        }
    }

其中“ProductCategory”是在此处传递的会话。在控制下我添加了以下代码:

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

就是这样:)

【讨论】:

  • 天哪! - 只需使用:return P_Pic != null ? $"../Image/Products/{ProductCategory}/{P_Pic}" : ""; ;)。
猜你喜欢
  • 1970-01-01
  • 2011-07-20
  • 2015-01-10
  • 2018-07-16
  • 2020-06-27
  • 2012-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多