Solved

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

Posted on 2011-03-16
4
830 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

632 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