ankit0311
ankit0311

Reputation: 735

Image of asp:ImageButton getting displayed using relative path, but not with absolute path

<asp:ImageButton runat="server" ID="cal_btn1"
        AlternateText="image" ImageUrl="~/Images/calendar_icon1.png"/>

The image gets displayed with the above code, but I have no idea what happens when I use the absolute path-

<asp:ImageButton runat="server" ID="cal_btn1"
        AlternateText="image" ImageUrl="C:\Users\adwivedi\Documents\Visual Studio 2010\WebSites\WebSite1\Images\calendar_icon1.png"/>

I replaced '\' with '\\', but still no change. Any idea what's wrong? Thanks!

Upvotes: 2

Views: 2858

Answers (1)

Claudio Redi
Claudio Redi

Reputation: 68400

It's not working because you have to use an url, not a physical path.

Use the ImageUrl property to specify the URL of an image to display in the Image control. You can use a relative or an absolute URL. A relative URL relates the location of the image to the location of the Web page without specifying a complete path on the server. The path is relative to the location of the Web page. This makes it easier to move the entire site to another directory on the server without updating the code. An absolute URL provides the complete path, so moving the site to another directory requires that you update the code.

Absolute Url

<asp:ImageButton runat="server" ID="cal_btn1"
    AlternateText="image" ImageUrl="http://mydomain/Images/calendar_icon1.png"/>

Relative Url

<asp:ImageButton runat="server" ID="cal_btn1"
    AlternateText="image" ImageUrl="Images/calendar_icon1.png"/>

Application Root Relative Url

<asp:ImageButton runat="server" ID="cal_btn1"
    AlternateText="image" ImageUrl="~/Images/calendar_icon1.png"/>

I suggest you to take a look to this MSDN article in relation to asp.net paths.

Upvotes: 3

Related Questions