+ Start a Discussion
harsh deep 1harsh deep 1 

I want a page block to be rendered as soon as i click a button in another page block..How can it be done?

Best Answer chosen by harsh deep 1
Nithesh NNithesh N
I made changes and tested the following code in my Dev Org.
It is working fine now.

VFP:
<apex:page controller="RenderController">
 <apex:form >
  <apex:commandButton value="Get Block1" action="{!renderB1}"/> 
  <apex:pageBlock id="block1" rendered="{!myb1}">
      <h1>Block 1 Rendered</h1>
  </apex:pageBlock>
 </apex:form>
</apex:page>

Controller:
public class RenderController {
    public Boolean myb1{get;set;}
    public RenderController(){
        this.myb1=false;
   }
    public PageReference renderB1() {
        if(this.myb1)
        {
            this.myb1=false;
        }
        else{
            this.myb1=true;
        }
       return null;
      }
}

Hope it helps...
If it solves your query, Please mark this solution as Best Solution to make this post 'Solved' . 

Best,
Nithesh.

All Answers

Prady01Prady01
Hi there, the easiest way to do this is using Jquery.

1) Make sure the part of section is invisble on the page load.
2) Attach Jquery click function to button, Onclick.
3) The above attached function and make it visible using jquery

jq = $.noConflict();
jq('#sectionId').css('display', 'block')

Hope this helps!


Thank you
Prady01
Nithesh NNithesh N
Hi Harsh, 
Use this Code as reference. 
You can follow the same logic for both standard and custom controller. 

Vf Page:
<apex:page standardController="Account" extensions="AccExtension">
    
    <apex:form>
    <apex:commandButton value="Render it" action="{!RenderIt}"/>
    </apex:form>
    
    <apex:pageBlock rendered="{!isReady}">
    	<h1>
            ta da....
        </h1>
    </apex:pageBlock>
    
</apex:page>

Controller Ext:
public class AccExtension {
    public Boolean IsReady {get;set;}
    public Account entry;
    
    public AccExtension(ApexPages.StandardController stdController) {
      	this.entry = (Account)stdController.getRecord();
        this.IsReady = false;
    }    
    
    public PageReference RenderIt(){
        this.isReady = true;
        System.debug( 'Button Clicked!!');
        return null;

    }
}


Hope it helps...
If it solves your query, Please mark this solution as Best Solution to make this post 'Solved' . 

Best,
Nithesh.
 
harsh deep 1harsh deep 1
Thanks guys for suggesting these methods.
As of now, i tried the one suggested by Nithesh, somewhat. But m not getting the functionality properly.Even if m clicking the button, the block content isn't getting displayed.
Here is what i implemented:

VFP:-
<apex:page controller="RenderController">
<apex:form >
<apex:commandButton value="Get Block1" reRender="block1" action="{!getB1}"/>
<apex:pageBlock id="block1" rendered="{!myb1}">
Block 1 Rendered
</apex:pageBlock>
</apex:form>
</apex:page>

Controller:-

public class RenderController {
    public Boolean myb1{get;set;}
    public RenderController(){
        this.myb1=false;
   }
    public void getB1() {
        if(this.myb1)
        {
            this.myb1=false;
        }
        else{
            this.myb1=true;
        }
      }
}
Please check once if i missed out anything.
Nithesh NNithesh N
Try ths....

VFP:
<apex:page controller="RenderController">
 <apex:form >
  <apex:commandButton value="Get Block1" reRender="block1" action="{!renderB1}"/> 
  <apex:pageBlock id="block1" rendered="{!myb1}">
    Block 1 Rendered
  </apex:pageBlock>
 </apex:form>
</apex:page>
Controller:
public class RenderController {
    public Boolean myb1{get;set;}
    public RenderController(){
        this.myb1=false;
   }
    public Boolean renderB1() {
        if(this.myb1)
        {
            this.myb1=false;
        }
        else{
            this.myb1=true;
        }
       return this.myb1;
      }
}
Hope it helps...
If it solves your query, Please mark this solution as Best Solution to make this post 'Solved' . 

Best,
Nithesh.

 
harsh deep 1harsh deep 1
Nithesh, it's giving me vf Error stating:
"Return type of an Apex action method must be a PageReference. Found: java.lang.Boolean "
If something needs to be pulled from controller class to vfp, the return type chould be PageReference.
 
Nithesh NNithesh N
Sorry !! My Mistake
 
public class RenderController {
    public Boolean myb1{get;set;}
    public RenderController(){
        this.myb1=false;
   }
    public PageReference renderB1() {
        if(this.myb1)
        {
            this.myb1=false;
        }
        else{
            this.myb1=true;
        }
       return null;
      }
}

Let me know if it works.

Best,
Nithesh
harsh deep 1harsh deep 1
Tried this also.
Still the issue persists..!!
Nithesh NNithesh N
I made changes and tested the following code in my Dev Org.
It is working fine now.

VFP:
<apex:page controller="RenderController">
 <apex:form >
  <apex:commandButton value="Get Block1" action="{!renderB1}"/> 
  <apex:pageBlock id="block1" rendered="{!myb1}">
      <h1>Block 1 Rendered</h1>
  </apex:pageBlock>
 </apex:form>
</apex:page>

Controller:
public class RenderController {
    public Boolean myb1{get;set;}
    public RenderController(){
        this.myb1=false;
   }
    public PageReference renderB1() {
        if(this.myb1)
        {
            this.myb1=false;
        }
        else{
            this.myb1=true;
        }
       return null;
      }
}

Hope it helps...
If it solves your query, Please mark this solution as Best Solution to make this post 'Solved' . 

Best,
Nithesh.
This was selected as the best answer
harsh deep 1harsh deep 1
Thanks Nithesh. It is working now.
So, can we imply that we need not mention "rerender" attribute in command Button component?? Because thats the only difference between this code n the previous one...

In that case, how could then "rerender" attribute be utilized?
Nithesh NNithesh N
Rerender can be utilized for table data...like pageblock table , which refreshes with new data. 
In a nut shell, we are not getting any new data from server (salesforce database), so rerender is not necessary. 
harsh deep 1harsh deep 1
okay..Thanks Nithesh.
harsh deep 1harsh deep 1
Nithesh,
I found a very unusual thing with this "Rerender" attribute.
When I include PageBlockSection component inside that page block, n use this attribute in this component, the previous code is working exactly fine.
VFP:-

<apex:page controller="RenderController">
<apex:form >
<apex:commandButton value="Get Block1" action="{!getB1}" />

<apex:pageBlock id="block1" >
    <apex:pageBlockSection title="Rendered Section" rendered="{!myb1}">
    </apex:pageBlockSection>

</apex:pageBlock>

</apex:form>
</apex:page>

Controller:-(same as earlier)

public class RenderController {
    public Boolean myb1{get;set;}
    public RenderController(){
        this.myb1=false;
    }
    public PageReference getB1() {
        if(this.myb1)
        {
            this.myb1=false;
        }
        else{
           this.myb1=true;
        }
       return null;
    }
}
harsh deep 1harsh deep 1
Sorry . Actually, its "rendered" attribute. NOT "rerender"