jre247
jre247

Reputation: 1897

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

Upvotes: 2

Views: 8353

Answers (2)

Icarus
Icarus

Reputation: 63970

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

Upvotes: 2

Mungoid
Mungoid

Reputation: 575

you can add runat="server" on the img tag and in the code behind you can put

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

Upvotes: 9

Related Questions