salesforceapexstatic-resource

Assigning Static Resource to apex:image from Apex class


I want to assign a static resource image based on a pick list selection to . Any idea how do I do that using apex class. I was able to achieve this using a javascript function but then I have a requirement to render this page as PDF which doesn't support javascript.

Any ideas/pointers ????


Solution

  • Use Apex:variable to assign image path dynamically.

    Here is a sample code.

    public class TestStaticResource{

    public string ImagePath{
        get;set;
    }
    public TestStaticResource(){
        ImagePath='images/yourImage.png';
    }
    

    }

     <apex:form>
          <apex:variable var="var" value="{!ImagePath}"/> 
          <apex:image url="{!URLFOR($Resource.StaticResuorceName, var)}"/> 
     </apex:form>