简体   繁体   中英

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). Here's the html of image:

<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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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