+ Start a Discussion
petermaagpetermaag 

Shared <apex:component> controller context

I have a visualforce page that is composed of several components. I set the controller for each component and the <apex:page> to the same class. I insert one of the instance objects into the database in one of the components. When I try to access the object ID of the object in another component, it is null. 

 

I get the feeling that each component is using a separate instantiation of the controller class. Is this the case? And more importantly, is there a way to allow separate components to share the same controller context?

 

Thanks.

Best Answer chosen by Admin (Salesforce Developers) 
SteveBowerSteveBower

Yes, you can pass the Page level controller to the Component controller as an Attribute, and then the Component has access to the Page level controller as well as it's own.  

 

theComponent:

 

 

 

<apex:component controller="ComponentController" id="theComponent">

 

<apex:attribute name="control" type="PageController" assignTo="{!topController}" description="Main page Controller"/>

 

 

Component Class:

 

 

public class ComponentController {

public PageController topController {get; set;}

....

        topController.variable_reference = ... etc.

 

 

Page:

...

<c:theComponent control="{!theController}" />

...

 

 

Page level Controller:

....

    public theController gettheController() {

    return this;

    }

 

 

 

Hope this helps, Steve.

 

 

All Answers

Shashikant SharmaShashikant Sharma

No there is no such way to my knowledge, I would love to see an example if anyone has done such.

SteveBowerSteveBower

Yes, you can pass the Page level controller to the Component controller as an Attribute, and then the Component has access to the Page level controller as well as it's own.  

 

theComponent:

 

 

 

<apex:component controller="ComponentController" id="theComponent">

 

<apex:attribute name="control" type="PageController" assignTo="{!topController}" description="Main page Controller"/>

 

 

Component Class:

 

 

public class ComponentController {

public PageController topController {get; set;}

....

        topController.variable_reference = ... etc.

 

 

Page:

...

<c:theComponent control="{!theController}" />

...

 

 

Page level Controller:

....

    public theController gettheController() {

    return this;

    }

 

 

 

Hope this helps, Steve.

 

 

This was selected as the best answer
petermaagpetermaag

That's what I needed. Thanks so much.