Solved

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

Posted on 2011-03-16
4
828 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

763 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