One of my friend was using apex:inputFile on a Visual force page, and he also needs to reRender some information on the same page. If we need to use both together, we have to use apex:actionRegion in order to isolate apex:inputFile (because inputFile doesn’t support reRender and throws error).
He was also doing the same thing but the state of information was not maintained, See the below psuedo code to get an idea of code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<apex:page> <apex:form> <!-- Action Region A --> <apex:actionRegion> <apex:actionFunction name="myFun" action="{!someActionofController}" reRender="myInfo"/> <!-- OTHER CODE --> </apex:actionRegion> <!-- Action Region B --> <apex:actionRegion> <apex:outputPanel id="myInfo"> <!-- Information Here which needs to sent/retrieve from server --> <!-- Calling Here myFun(), which reRender myInfo --> </apex:outputPanel> </apex:actionRegion> </apex:form> </apex:page> |
You will think that the above code will work fine, But It won’t!
Issue In the Above Code
Let’s say you are using a list of contact in editable mode where user can add or remove rows and update the information of contact.
User has added one row and filled the information, Now he clicked on some button to add one more row. To add row, function “myFun” (placed in actionRegion 1) is invoked. Here the information for first row of contact will go away on reRender of myInfo panel.
Solution/ Workaround
As of now, after doing some research and hit and trial approach I have found one workaround or you can say solution. Instead of putting apex:actionFuntion and your part which needs to reRendered put them in a single actionRegion. So the updated code (working) will be as follows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<apex:page> <apex:form> <!-- Action Region A --> <apex:actionRegion> <!-- OTHER CODE --> </apex:actionRegion> <!-- Action Region B, This will have both actionFunction and information which needs to reRendered --> <apex:actionRegion> <apex:actionFunction name="myFun" action="{!someActionofController}" reRender="myInfo"/> <apex:outputPanel id="myInfo"> <!-- Information Here which needs to sent/retrieve from server --> <!-- Calling Here myFun(), which reRender myInfo --> </apex:outputPanel> </apex:actionRegion> </apex:form> </apex:page> |
Reason/Explanation
For now there is no 100% correct explanation behind this behavior. May be actionFunction sends only information of apex:actionRegion in which it is placed to the server. For example if actionFunction is in actionRegion A, then it will send information from actionRegion A to the server. May Be my assumption is wrong. So for now no solid reason, but we have solution. So for now just solve this issue and move faster to meet your deadlines.
HI
I am trying for creating rows by clicking add button and when cliking add button the first rows data is lost and the selected file also resetted.
Can you please help where i am missing? Thanks
VPF:
function submitData() {
if(document.getElementById(‘enmsg’)!=null)
{
var uname = document.getElementById(‘enmsg’).value;
var password = document.getElementById(‘esmsg’).value;
submitActionFunction(uname,password);
}
{
submitActionFunction(“”,””);
}
}
Class:
public class ResourceDocComponent {
public integer lstCnt{get;set;}
public List lstRscDoc {get;set;}
public List lstRscDoc2 {get;set;}
public string en{get;set;}
public string es{get;set;}
public void init(){
system.debug(‘called init’);
}
public void ResourceDocComponent(){
lstRscDoc = new List();
DocWarpper dc=new DocWarpper();
lstRscDoc.add(dc);
system.debug(lstRscDoc.size());
}
public void submitForm()
{
System.debug(‘## User name: ‘+en);
System.debug(‘## Password: ‘+es);
if(lstRscDoc ==null)
{
lstRscDoc = new List();
}
else
{
system.debug(‘Not null’);
}
DocWarpper dc=new DocWarpper();
dc.EnText=en;
dc.EsText=es;
lstRscDoc.add(dc);
}
public void addRow2(){
if(lstRscDoc2 ==null)
{
lstRscDoc2 = new List();
}
DocWarpper2 dc=new DocWarpper2();
lstRscDoc2.add(dc);
}
public void addRow(){
if(lstRscDoc ==null)
{
lstRscDoc = new List();
}
else
{
system.debug(‘Not null’);
}
system.debug(‘#########’);
system.debug(lstRscDoc.size());
system.debug(‘#########’);
system.debug(‘@@@@@@@@@@’);
for(DocWarpper con : lstRscDoc){
system.debug(con.EnText);
}
system.debug(‘@@@@@@@@@@’);
DocWarpper dc=new DocWarpper();
lstRscDoc.add(dc);
}
public class DocWarpper2
{
public Attachment myfile{get;set;}
public WelcomeDoc__c dc {get;set;}
public DocWarpper2()
{
myfile=new Attachment();
dc = new WelcomeDoc__c();
}
}
public class DocWarpper
{
public Attachment myfile{get;set;}
public String EnText {get;set;}
public String EsText {get;set;}
public DocWarpper()
{
myfile=new Attachment();
}
}
}