Path in a control in a master page does not work!

G

Guest

I can use a good help here with what looks like a simple problem.

I have a control called Header.ascx in /Control/System directory, where / is
my web application directory.

The header is used in a master page called MasterPage.master in the /
directory.

I have a Default.aspx in / which uses the master page in the root. The user
control (which is really in the master page) in this page shows fine.

The problem is /directory1/earthquake.aspx. This page also uses the master
page in the / directory.

When this page is displayed, the control does not show like the Default.aspx
in the / directory.

So, I tried changing <img src="image/abc.jpg"> to <img
src="~/image/abc.jpg>, but the images in the user control, which is used in
the master page does not show at all.

What am I doing wrong?
 
S

Sean Chambers

ahh, I ripped my hair out quite awhile over this problem. It's quite
annoying.

Here's the catch, if you want to use the ~ in your img src's, you have
to tell asp to render the paths for the img src at the server, so...you
have to do: <img src="~/path/to/image.jpg" runat="server" />. This will
make asp render out the correct path to your images at runtime and the
images should show up.

Another way of doing it is (which is what I did), is to set a property
in your site.master called something like "ImagePath", and do it like
so:

<img src="<%=ImagePath%>image.jpg" />

and then in your code-behind you would have:

public static string ImageFolder {
get {
return HttpContext.Current.Request.ApplicationPath +
"img/";
}
}

That will work as well. I'm sure everyone has a different way of doing
it.

hope that helps!

Sean
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top