简体   繁体   English

ImageURL上的c#代码没有显示图像

[英]ImageURL on c# code not showing image

I have image on aspx page as: 我在aspx页面上有图像:

<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. 我检查文件夹OrgImages包含指定的图像。

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. 请帮我。

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 . 我添加了runat="server"因此您可以在codebehind中访问<img ID并设置src

Example: MainContent_imgOrgLogo.Src = (YOUR IMAGEPATH) 示例: MainContent_imgOrgLogo.Src = (YOUR IMAGEPATH)

Or try (since you are talking about a ddlOrganization_SelectedIndexChanged ): 或尝试(因为你在谈论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. 如果你想实现这一点,你应该将<img -attribute放在UpdatePanelddlOrganization_SelectedIndexChanged -event中,你应该粘贴.ImageURL

Change the line 改变线

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

with

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

and remove 并删除

imgOrgLogo.DataBind();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM