Click here to Skip to main content
14,420,468 members
Rate this:
Please Sign up or sign in to vote.
Hi All,

I have html button tag in which I have nested the image tag to show in my application.

The problem is when I use relative path the image tag does not work at all but if I use absolute path it works just fine.

But I do not want to use absolute path because I need to change it if I put this on test server and on main server.

Here is code with absolute path:
treeNode.Append(@"<button ><img src=""http://localhost:62473/mainFolder/Images/image1.png"" width=""30"" height=""15"" alt=""Get Info""></button>");


Here is code with relative path:


treeNode.Append(@"<button ><img src=""~/Images/image1.png"" width=""30"" height=""15"" alt=""Get Info""></button>");



Please help me and let me know where I am going wrong?
Posted

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Of course it always works. You did no try use relative path anywhere, so I don't know where you screwed it up. The path should be relative the the HTML file using the path. Perhaps you don't quite know the path syntax: '.' is "this directory", '..' means one level up, '~' means root directory set up for your site, and so on.

—SA
   
Comments
Member 11842305 20-Jul-15 16:39pm
   
I have used ~ this because my Images folder is in root directory. Should I use another one?
Sergey Alexandrovich Kryukov 20-Jul-15 16:50pm
   
No, if this is what you use, it should work. Just check up everything accurately.
By the way, specifying both width and height is not maintainable, use none of them (if you can) or only width or only height, otherwise you can mangle you aspect ratio.
—SA
Member 11842305 20-Jul-15 16:52pm
   
It's not working, No image is showing with the relative path code.:(
Sergey Alexandrovich Kryukov 20-Jul-15 16:55pm
   
Is it working an any way at all? Check up if the image is correct, has correct location, everything. It's only a matter of accurate work. After all, show the image on a page loaded in browser, without a server, to start with something.
—SA
Member 11842305 20-Jul-15 16:57pm
   
Yeah as I mentioned it's working just fine with absolute path but not with the relative. There is no problem with the image.
Member 11842305 20-Jul-15 16:57pm
   
Yeah as I mentioned it's working just fine with absolute path but not with the relative. There is no problem with the image.
Sergey Alexandrovich Kryukov 20-Jul-15 17:01pm
   
Show:
1) img src="..." with absolute path (relative to site root), the way it shows;
2) path of HTML or ASP.NET file using it
—SA
Member 11842305 20-Jul-15 17:15pm
   
I am sorry Sir but I didn't get you?
Sergey Alexandrovich Kryukov 20-Jul-15 17:30pm
   
Just give me all paths involved, relative to your site root ~:
1) image;
2) HTML using that image.
—SA
Member 11842305 20-Jul-15 17:33pm
   
Hello Sir,

as you suggested I tried ../myfolder/image name and it worked just fine in test server also. Thank you so much for your help. you are awesome
Sergey Alexandrovich Kryukov 20-Jul-15 18:19pm
   
You are welcome.
—SA
Member 11842305 20-Jul-15 16:53pm
   
I will remove the width and height attributes.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100