When i set the image with an url in code behind it doesn't work , i don't know why ?
if (File.Exists(Server.MapPath("~/Images/EmpQr/") + int.Parse(Session["userID"].ToString()) + ".PNG")) //It passes this condition
{
tr_res.Visible = true;
img_res.ImageUrl = Server.MapPath("~/Images/EmpQr/" + int.Parse(Session["userID"].ToString()) + ".PNG"); //Here 's the problem ,no image
}
else
{
tr_res.Visible = false;
}
<asp:Image ID="img_res" runat="server" AlternateText="result" />
When i set image url like this
ImageUrl ="~/Images/EmpQr/1345.PNG"
in the design view it works .
How to fix this problem ?
Server.MapPath
gives you local path to the file. While what you really want is the relative to the application root path (on the server!). For this what you already have,~/Images/EmpQr/
is fine, so just append file name to it:Update. Out of curiosity, after discussion in comments, here is the relevant part of
Image
control source code, which proves that url in form of~/Images/...
will be handled correctly: