Solved

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

Posted on 2011-03-16
4
829 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
YouTube API get the Video Tags - vb.net 7 55
Multi-Column Repeater 3 39
Returning multiple values with a stored procedure 9 31
Need a quick overview of Selenium testing 2 36
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

710 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