Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now