简体   繁体   English

如何从代码后面更改源图像属性

[英]How to change source image property from code behind

I'm wondering how to change a color property of an image from code behind of same page. 我想知道如何从同一页面后面的代码更改图像的颜色属性。 Below, I have html code for the image that I want to change to yellow if file is not contained in important announcements folder (this is checked in code behind). 下面,如果文件没有包含在重要的公告文件夹中,我有想要更改为黄色的图像的html代码(这在代码后面检查)。 Here's the html of image: 这是图像的html:

<li class="levelOne"><a class="button notice buttonEight" href="#">
<img id="importantImg" src="<%= Page.ResolveUrl("~/{0}/_res/_images/icon_notice.png",      
PBS.Cms.Settings.PBSFolderName) %>" /></a></li>

Here's a snippet of the code behind for this page: 以下是此页面背后的代码片段:

//validate folder is important announcements
if (!cd.FolderName.Equals("Important Announcements"))
{
//string folderName = cd.FolderName.ToString();
Response.Write("folder doesn't equal Important Announcements");
}

Any help? 有帮助吗?

Thanks! 谢谢!

Jason 贾森

你可以在img标签上添加runat =“server”,然后在你可以放的代码中添加

importantTag.Attributes["src"] = "yourNewImageUrl";

If you are going to do it from the markup, I think you need this instead: 如果你打算从标记中做到这一点,我认为你需要这样做:

<li class="levelOne"><a class="button notice buttonEight" href="#">

   <img id="importantImg" 
   src="<%= Page.ResolveUrl(string.Format("~/{0}/_res/_images/icon_notice.png",      
   PBS.Cms.Settings.PBSFolderName)) %>" /></a>

</li>

But instead, you could do it completely from code behind. 但相反,你可以完全从代码背后做到。 Having the image declared like this: 将图像声明为:

 <img id="importantImg" runat="server" src=""  />

You can do this on code behind: 您可以在后面的代码上执行此操作:

importantImg.src=Page.ResolveUrl("relative/path/to/image");

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

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