Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relative path from a root operator ("~") address in code behind

Tags:

asp.net

Server controls like Image.ImageUrl make this very easy, but trying to achieve the same thing in code behind to an IMG html control is not that straightforward.

For example using an Asp:Image server control and setting ImageUrl property to "~/Images/Test.jpg" works fine no matter what directory i place the code in. (asp.net transforms tilde directory to relative)

How can i do the same in code behind? when i am trying to create an HTML IMG control?

like image 788
Nikos Tsokos Avatar asked Sep 23 '09 09:09

Nikos Tsokos


2 Answers

Use Page.ResolveUrl("~/.....");

like image 56
Pitming Avatar answered Nov 08 '22 22:11

Pitming


In the template:

<img id="imgTest" runat="server" />

In the codebehind:

imgTest.Attributes["src"] = this.ResolveUrl("~/yourimage.gif");

Does this solve your problem? Not really sure why you're not using an asp:image control...

like image 29
James McCormack Avatar answered Nov 08 '22 22:11

James McCormack