2

I have a page that give the possibility to insert two pictures to the users. This is a page in a section that it is part of a custom object. Everything works fine except that when inserting pictureOne the page refresh and the only thing I see is the picture that is inserted and the apex:pageMessage. I was hoping to see the page exactly as I presented to the user bebore the pictureOne is inserted. When doing this with pictureTwo, everything works fine, the picture is inserted and stays in the page and all other parts of the Visualforce page are there. I think that there is something wrong with the rendered I'm using, but can't really figure out what it is I'm doing wrong. Any help will be much appreciated.

<apex:page StandardController="Whatever__c"  extensions="AttachmentController" showHeader="false">
 <apex:form id="theForm">
   <apex:pageBlock > <apex:pageMessages />
       <table>
         <tr>
          <td>
          <apex:outPutPanel id="imagePanel" rendered="{!FileId!=null}">
             <img id="theImage"  src="/servlet/servlet.FileDownload?file={!FileId}" style="vertical-align:left" width="250" height="300" onclick="window.open('/servlet/servlet.FileDownload?file={!FileId}');"/>
          </apex:outPutPanel> 

          <apex:outPutPanel id="thePanel" rendered="{!con == false}">
             <apex:pageBlockSection >
             <apex:inputFile value="{!pictureOne.body}" filename="{!pictureOne.name}"/>
             <apex:commandButton value="Save" action="{!savepictureOne}" />
             </apex:pageBlockSection>
          </apex:outPutPanel>

          </td>
        <td>
          <apex:outPutPanel id="imagePanel2" rendered="{!FileId2!=null}">
                  <img id="theImage2"  src="/servlet/servlet.FileDownload?file={!FileId2}" style="vertical-align:left" width="250" height="300" onclick="window.open('/servlet/servlet.FileDownload?file={!FileId2}');"/>
             </apex:outPutPanel> 

           <apex:outPutPanel id="thePanel2" rendered="{!con == false}">
               <apex:pageBlockSection >
               <apex:inputFile value="{!pictureTwo.body}" filename="{!pictureTwo.name}"/>
               <apex:commandButton value="Save" action="{!savepictureTwo}" />
               </apex:pageBlockSection>
           </apex:outPutPanel>

            </td>
         </tr>
       </table>
    </apex:pageBlock>
</apex:form>

Controller ----------

   public with sharing class AttachmentController {

   String recId;
   public boolean con { get; set; }
   public boolean con2 { get; set; }

   public AttachmentController(ApexPages.StandardController controller) {
   recId = controller.getId();  


    }

   public Attachment pictureOne {
       get {
          if (pictureOne == null)
          pictureOne = new Attachment();
          return pictureOne;
           }
      set;
  }


  public PageReference savePictureOne() {

    pictureOne.OwnerId = UserInfo.getUserId();
    pictureOne.ParentId = ApexPages.currentPage().getParameters().get('id');
    pictureOne.Name = 'W1' + pictureOne.name; 



    try {

      insert pictureOne;
      con = true;


    } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
      return null;
    } finally {
      pictureOne = new Attachment(); 


    }

    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Attachment uploaded successfully'));
    return null;

    }    

    public String getFileId() {
        String fileId = '';
        List<Attachment> attachedFiles = [select Id from Attachment where Name LIKE 'W1%' AND parentId =:recId order By LastModifiedDate DESC limit 1];
        if( attachedFiles != null && attachedFiles.size() > 0 ) {
            fileId = attachedFiles[0].Id;
        }
        return fileId;    
    }

    public Attachment pictureTwo {
       get {
          if (pictureTwo == null)
          pictureTwo = new Attachment();
          return pictureTwo;
           }
      set;
    }

    public PageReference savePictureTwo() {

    pictureTwo.OwnerId = UserInfo.getUserId();
    pictureTwo.ParentId = ApexPages.currentPage().getParameters().get('id');
    pictureTwo.Name = 'W2' + pictureTwo.name;



    try {

      insert pictureTwo;
      con2 = true;


    } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
      return null;
    } finally {
      pictureTwo = new Attachment(); 


    }

    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Attachment uploaded successfully'));
    return null;

    }    

    public String getFileId2() {
        String fileId2 = '';
        List<Attachment> attachedFiles = [select Id from Attachment where Name LIKE 'W2%' AND parentId =:recId order By LastModifiedDate DESC limit 1];
        if( attachedFiles != null && attachedFiles.size() > 0 ) {
            fileId2 = attachedFiles[0].Id;
        }
        return fileId2;    
    }
}

Also if you guys think that there is something that I can improve on my code let me know. Thanks.

1 Answer 1

2

In your second output panel you are using con rather than con2 to decide whether to render. As con is set to true when picture 1 is uploaded, this panel won't be displayed:

<apex:outputPanel id="thePanel2" rendered="{!con==false}">
  <apex:pageBlockSection >
      <apex:inputFile value="{!pictureTwo.body}" filename="{!pictureTwo.name}"/>
      <apex:commandButton value="Save" action="{!savepictureTwo}" />
  </apex:pageBlockSection>
</apex:outputPanel>

If you change the first line to:

<apex:outputPanel id="thePanel2" rendered="{! con2==false}">

then the panel will display until picture 2 is uploaded.

7
  • Thank you Bob, that was the little detail I was missing :( .. Any tips on the overal visualforce page and code? Do you think this is a decent code apporach? Anyone? Thanks! Commented Mar 19, 2014 at 8:58
  • Is there a particular reason why you use con/con2 to render the input panels, but check fileid to render the images? I would think you could use fileid/fileid2 for both and render the input if null and the image if not null. Commented Mar 19, 2014 at 9:09
  • Bob are you saying that I can get the same results from this: <apex:outPutPanel id="thePanel" renderd="{!con==false}"> if I change it to this?:<apex:outPutPanel id="thePanel" renderd="{!FileId==null}"> Commented Mar 19, 2014 at 9:39
  • Yes - the image only renders if fileId != null, so the input will only render if fileId == null. Commented Mar 19, 2014 at 9:47
  • Hello again Bob. If I do that I can't see any panels, no Browse buttons, nothing. Commented Mar 19, 2014 at 13:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .