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?
Use Page.ResolveUrl("~/.....");
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With