Search code examples
c#asp.netdatalistitemtemplate

Image Control dosen't work


I have the Following ASP code:

<asp:DataList ID="dlGallery" runat="server"  
    <ItemTemplate>
        <div style="float: left; padding-right: 10px; text-align: center">
            <img src="<%= ApplicationPath%><%# Eval("ImageUrl") %>"width="80"/>               
    </ItemTemplate>
</asp:DataList> 

I get this Error: Compiler Error Message: CS0103: The name 'ApplicationPath' does not exist in the current context

What is it mean?What may be problem with ApplicationPath?

Thank You in Advance


Solution

  • One of the first .NET tricks I learnt to simply prepend <%= Request.ApplicationPath %> to the beginning of any paths in your pages, such as:

    /Images/Foo.gif' ... />

    The problem is that, when a file is requested, it is returned "root-relative" without a trailing slash. This means that when a page is requested from the root folder of a Website, Request.ApplicationPath returns '/'. In the above example the following string will be written out:

    This wouldn't be picked up when developing against localhost because the application will be one level under the root of the Website meaning that the virtual directory name will be returned after the slash '/':

    However, if a page is requested from a page in a sub-folder, then the path is returned as "/FldrName" and the above example will resolve correctly as:

    ' From the root ' In a virtual directory