2
<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!

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

9 Comments

If I use the relative Url "Images/calendar_icon1.png", it should work when I host the site using IIS in a different location, right? But that is not what happens!
@ankit0311: It depends of where the page using this control is placed in relation to images folder. You could add more details about your files hierarchy or maybe could just use the absolute url.
@ankit0311: that isn't anything to do with what you asked above though. The reason your absolute path doesn't work is because of the reason Claudio gave. IF you are having problems with image visibility when deploying you should ask that as a separate question.
@Claudio Redi: Not worrking even when I use the absolute url. I am really frustrated by this! :(
@ankit0311: if the absolute url is not working, then image might not be there. Double check that.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.