ImageURL on c# code not showing image

11,060

Solution 1

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 the <img ID in codebehind and set the src.

Example: MainContent_imgOrgLogo.Src = (YOUR IMAGEPATH)

Or try (since you are talking about a ddlOrganization_SelectedIndexChanged):

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

Edit:

but on selection it should change the image.

If you want to achieve that, you should put the <img-attribute inside a UpdatePanel and on the ddlOrganization_SelectedIndexChanged-event you should paste your .ImageURL-code.

Solution 2

Change the line

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

with

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

and remove

imgOrgLogo.DataBind();
Share:
11,060
C Sharper
Author by

C Sharper

Updated on June 08, 2022

Comments

  • C Sharper
    C Sharper almost 2 years

    I have image on aspx page as:

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

    I have ready path for it in database, and I am fetching image name from database and setting up its path as:

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

    from string path I get the image name.

    I checked folder OrgImages contains specified image.

    But image is not viewing after running this code.

    When i done inspect element from browser its showing:

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

    Path is not getting settled.

    What is wrong in my code??

    Please help me.

  • C Sharper
    C Sharper about 10 years
    but on selection it should change the image...in this case it will not
  • Abdul Basit
    Abdul Basit about 10 years
    show me the html of href attribute of image generated by browser
  • C Sharper
    C Sharper about 10 years
    <img id="MainContent_imgOrgLogo" src="" alt="Image Not Found" style="height:35px;width:50px;">
  • Abdul Basit
    Abdul Basit about 10 years
    well your ImageUrl attribute may be ovride by other code in your code behind.
  • Abdul Basit
    Abdul Basit about 10 years
    remove imgOrgLogo.DataBind(); it will work with out updatepanel