ImageURL in C # code not displaying an image

I have an image on an aspx page like:

<asp:Image ID="imgOrgLogo" runat="server" Width="50px" Height="35px" AlternateText="Image Not Found"  />

I have a ready path for it in the database, and I get the image name from the database and setting its path as:

 string path = obj.ExecuteScalar(sql);   
 imgOrgLogo.ImageUrl = "/OrgImages/" + path;
 imgOrgLogo.DataBind();

from line to line I get the image name.

I checked the OrgImages folder contains the specified image.

But after running this code, the image is not visible.

When I checked the item from the browser, it shows:

   <img id="MainContent_imgOrgLogo" src="" alt="Image Not Found"

  style="height:35px;width:50px;">

The path does not pass.

What is wrong in my code?

Please help me.

+4
source share
2 answers

Try:

<img id="MainContent_imgOrgLogo" src="" alt="Image Not Found" style="height:35px;width:50px;" runat="server" />

I added runat="server"so you can access <img IDin codebehind and install src.

Example: MainContent_imgOrgLogo.Src = (YOUR IMAGEPATH)

( ddlOrganization_SelectedIndexChanged):

if(!IsPostBack)
{
    string path = obj.ExecuteScalar(sql);   
    imgOrgLogo.ImageUrl = "/OrgImages/" + path;
    imgOrgLogo.DataBind();
}

Edit:

.

, <img UpdatePanel ddlOrganization_SelectedIndexChanged -event .ImageURL -.

+3

imgOrgLogo.ImageUrl = "/OrgImages/" + path;

imgOrgLogo.ImageUrl = "~/OrgImages/" + path;

imgOrgLogo.DataBind();
+3

Source: https://habr.com/ru/post/1533688/


All Articles