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.