Sett absolute path problem

Discussion in 'ASP .Net' started by Pumkin, Oct 12, 2006.

  1. Pumkin

    Pumkin Guest

    Hello guys,

    I have a small problem with setting the absolute path of an image.
    Here is the scenario:

    I need to put the image in a text concat something like this:

    <asp:Label id = "lbl" runat = "server" text = "<img id = 'img' src =
    'Images/picture.gif' /> text " />

    If I put a relative path everything is perfect, but if I try an
    absolute path it is not working.
    I have already tried:

    <asp:Label id = "lbl" runat = "server" text = "<img id = 'img' src =
    '/Images/picture.gif' /> text " />
    <asp:Label id = "lbl" runat = "server" text = "<img id = 'img' src =
    '~/Images/picture.gif' /> text
    runat = 'server' " />

    It seems like an easy thing to do but I cannot make it work.

    Thanks for the help...
    Pumkin, Oct 12, 2006
    1. Advertisements

  2. '~/Images/picture.gif' will look for picture.gif in

    In case, if you are using a virtual directory set up, your image will
    actually be in '<rootweb>\<virtual directory>\images\picture.gif'.

    Hope this helps.

    Augustin Prasanna, Oct 12, 2006
    1. Advertisements

  3. Pumkin

    Pumkin Guest

    Thank you for your interest and for the reply.

    The problem is that if I put :

    Text="<img id='img1' runat = 'server' src = '~/Images/picture.gif' />

    and if I look in View Source in IE I can see that it doesn't put any
    path at all:

    Text="&lt;img id = 'img1' src='~/Images/picture.gif' alt='' runat =
    'server' /&gt; Text"

    And if I put the realtive path it understands the path corectly.

    Any ideas?
    Pumkin, Oct 12, 2006
  4. Pumkin

    Pumkin Guest

    Lets say that my web site is on localhost and has the virtual directory

    Of course if I set the path src '
    "http://localhost/MyWebSite/Images/picture.gif" it is working.
    But I was wondering if there isn't a better way to do this.

    Pumkin, Oct 12, 2006
  5. it will not put any path if you use '~\Images'. It knows where to look for..

    Can you try putting in '..\Images\picture.gif' and tell me if this works?

    Augustin Prasanna, Oct 12, 2006
  6. Pumkin

    Pumkin Guest

    If I put '..\Images\picture.gif' it works.

    But the img is in a user control and it is used in 2 pages.
    And depending on the page that it is used I need to have as path
    '..\Images\picture.gif' and '..\..\Images\picture.gif'. So I cannot
    really used a relative path. :)
    Pumkin, Oct 12, 2006
  7. in this case, you can use ~\Request.ApplicationPath\Images\Picture.gif.. you
    can set this from code behind.. Hope this helps.
    Augustin Prasanna, Oct 12, 2006
  8. Pumkin

    Pumkin Guest

    Thank you very much for the solution offered.
    But I would rather not put the path from code-behind if possible (it is
    really difficult to do this in my situation).
    Is there other solution (from HTML) ??
    Sorry for so many questions here...

    Pumkin, Oct 12, 2006
  9. Thank you very much for the solution offered.
    If you realy need this weird construction, you can use data-binding:

    <asp:Label ID="MyLabel" Text="<%# "<img src='" +
    ResolveUrl("~/images/myImage.gif") + "' alt='xxx' />" %>" runat="server" />

    ....and don't forget to call MyLabel.DataBind() or Page.DataBind() form code.

    Robert Haken [MVP ASP/ASP.NET]
    HAVIT, s.r.o.,
    Robert Haken [MVP], Oct 12, 2006
  10. Pumkin

    Pumkin Guest

    It seems it is hopeless.. In the situation that I am having I cannot
    use server directives to set the path.
    To detail a little bit more the control in which I want to put the
    image is not a label,
    but the atlas control expandable panel and it doesn't allow me to put
    server code.

    Thanks for the help everybody anyway.
    Pumkin, Oct 12, 2006
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.