Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# eval中继器中的图像文件夹图片_C#_Asp.net - Fatal编程技术网

C# eval中继器中的图像文件夹图片

C# eval中继器中的图像文件夹图片,c#,asp.net,C#,Asp.net,当我在没有任何文件夹的情况下使用照片时,,它会起作用 但是,当我使用“图像”文件夹下的照片时,不会显示这些照片,我只能看到一个空白屏幕。以下是我正在使用的代码: <div class="wrapper"> <asp:Repeater runat="server" ID="Repeater1"> <ItemTemplate> <asp:Image ID="Image1"

当我在没有任何文件夹的情况下使用照片时,
,它会起作用

但是,当我使用“图像”文件夹下的照片时,不会显示这些照片,我只能看到一个空白屏幕。以下是我正在使用的代码:

<div class="wrapper">
    <asp:Repeater runat="server" ID="Repeater1">
        <ItemTemplate>
                <asp:Image ID="Image1" 
                        ImageUrl='"/Images" + <%# Eval("PresidentPhotoPath") %>' 
                        runat="server" />
        </ItemTemplate>
    </asp:Repeater>
</div>

我建议使用代码隐藏方法为您构建字符串,如下所示:

protected string BuildPath(string photoPath)
{
    return "Images/ + photoPath;
}
ImageUrl='<%# BuildPath(Eval("PresidentPhotoPath")) %>'

注意:考虑这个东西比BuffdPosith/Cuff>更有用,因为这是相当通用的,只是选择了这个名字,因为没有更好的东西马上出现。 现在,您可以在标记中调用该方法,如下所示:

protected string BuildPath(string photoPath)
{
    return "Images/ + photoPath;
}
ImageUrl='<%# BuildPath(Eval("PresidentPhotoPath")) %>'
ImageUrl=''
我建议采用这种方法,原因如下:

  • 标记不包含任何复杂的逻辑,只包含带有
    Eval()
    值的方法调用
  • 与嵌入式代码块相比,调试逻辑更容易
  • 当逻辑嵌入到标记的绑定语法中时,您可以利用VisualStudio编译器的强大功能捕获编译时的语法错误,而不是运行时的错误

将asp:Image更改为下面的代码:

<asp:Image ID="Image1" 
     ImageUrl='<%# string.Format("~/Images/{0}", Eval("PresidentPhotoPath")) %>' 
     runat="server" />