Solved

C#.NET  VS 2008 -- Can't show images on Master Page in subfolder.

Posted on 2011-03-16
4
827 Views
Last Modified: 2012-05-11
I'm using VS 2008, and can't seem to figure a way to have an image path work in both the root and sub folder .. when I get to the subfolder, it generates a link to the image within the subfolder

For instance -- if the image is Mid2.gif
In the main folder the link is correct : localhost/image/Mid2.gif
In the sub folder the link is incorrect as : localhost/subfolder/image/Mid2.gif


I need the abolute link to show up all the time ...
How can I make the link show correctly regardless of the subfolder
<tr><img src="images/mid2.jpg></tr>

Open in new window

0
Comment
Question by:dantheanswerman
  • 2
  • 2
4 Comments
 
LVL 7

Expert Comment

by:Element1910
ID: 35151364
The link in the subfolder page should be this, if the images folder is in the root.

<tr><img src="../images/mid2.jpg></tr>

Open in new window


0
 
LVL 7

Expert Comment

by:Element1910
ID: 35151378
Also, to give you a tip on this...the best way to get the absolute link to something is drag the image (from Visual Studio via your images folder), to the page you want it to show up on....no matter where the page is, VS will generate the proper link for you using this method.
0
 
LVL 2

Accepted Solution

by:
dantheanswerman earned 0 total points
ID: 35165850
Thanks Element -- The trick is that this is from the master page --which is relative to the page that is being referenced by the master page.

I finally got an answer -- not perfect maybe, but here it is -- direct out (via <%= tags to a method called GetUrl (string strImage) that takes the hard coded image "/images/header1.gif" as a param. The method calls  
C# Example --
In the html add:

<img src='<%= GetUrl("/images/spacer.gif") %>'  />

In the code behind (

Add "using System.Web;" to your using statements.

Add the following method:
    public string GetUrl(string strImage)
    {
        // Get the root url of your site -- this will allow you to reference an image regardless of the subfolder your page is in.
        string folderPath = HttpContext.Current.Request.ApplicationPath;
        return folderPath + strImage;
    }

Open in new window

C# Example --
In the html add:

<img src='<%= GetUrl("/images/spacer.gif") %>'  />

In the code behind (

Add "using System.Web;" to your using statements.

Add the following method:
    public string GetUrl(string strImage)
    {
        // Get the root url of your site -- this will allow you to reference an image regardless of the subfolder your page is in.
        string folderPath = HttpContext.Current.Request.ApplicationPath;
        return folderPath + strImage;
    }

Open in new window

0
 
LVL 2

Author Closing Comment

by:dantheanswerman
ID: 35196652
I guess I can't assign points to myself for doing my job ... but thanks for the help.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question