Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Can I pass a email value from a visualforce component to a Visualforce page? I have used components for reusing Company's logo and few images...

<apex:component>
    <apex:image value="{!$Resource.Logo}"> </apex:image>
</apex:component>

Is it possible to pass variable to a visualforce page?

Thanks.

share|improve this question

3 Answers 3

up vote 3 down vote accepted

The easiest way is to pass in the controller of your visualforce page. This means you'll need either a custom controller or an extension. When you pass in the controller, you can then call getters or setters on the controller given they are publicly accessible.

For example:

<apex:component>
    <apex:attribute name="ctrl" description="My controller" type="MyControllerClassName" required="true"/>
    <apex:image value="{!URLFOR(ctrl.ImageUrlGetter)}"> </apex:image>
</apex:component>
share|improve this answer

There is an extensive blog post on the Developerforce website here regarding this matter to help you out.

As a summary - you should pass in the controller of your page as an apex attribute of your component (just as James mentions). You will have your component look like

<apex:component controller="MyComponentController" >
    <apex:attribute name="pageController" type="PageControllerBase" assignTo="{!pageController}" required="true" description="The controller for the page." />
</apex:component>

Which on your page you will use as

<c:myComponent pageController="{!pageCont}" />

This will then assign the page controller returned in the "getPageCont" method on your page controller to the "pageController" parameter/property in your component. A method in your page's controller to return itself is needed for the "getPageCont" call.

public PageControllerBase getPageCont()
{
    return this;
}
share|improve this answer
1  
Can you summarise here? –  Keith C Apr 8 at 13:14
    
Done :-) Uses snippets from the link. –  pbattisson Apr 8 at 13:29
    
What is pageCont in the component? –  MnZ Apr 8 at 13:35
    
Should be a bit clearer now hopefully - you are assigning the pageControllerBase instance used as the controller of your page to the pageController parameter in the component using an intermediary getPageCont method. –  pbattisson Apr 8 at 13:38
    
@pbattisson Yes it is much clearer now. Thanks. –  MnZ Apr 9 at 8:02

If you feel passing the entire controller of your visualforce page is a bit too heavy-weighted, or if you need a generic component that can be reused across pages, you can also use a dedicated class to carry the data that you need to transport back and forth between page and component.

I.e. create a 'data' class like this:

public with sharing class ComponentData {
    public String theInput {get;set;}
    public String theOutput {get;set;}
}

In your page controller, instantiate the class:

public ComponentData theData {get;set;}

On your component, use the class as an attribute:

<apex:component controller="CompController">
<apex:attribute type="ComponentData" name="compData" required="true" description="The data going in and out" assignTo="{!cData}"/>

<apex:outputPanel id="banManagerPanel">
    <apex:inputText value="{!cData.input}">
        <apex:actionSupport event="onchange" action="{!updateData}"/>
    </apex:inputText>
</apex:outputPanel>
</apex:component>

On the component controller you can perform whatever logic that needs to be done:

public with sharing class ComponentController {
public ComponentData cData {get;set;}
public void updateData(){
    cData.theOutput = cData.theInput + ' with something extra';
}
}

And finally, on the VF page pass the instantiated data-class to the component:

<c:theComponent compData="{!theData}"/>
<apex:outputText value="{!theData.theOutput}" />

The theData on the VF page will get the updated value that was modified by the component controller, after the button inside the component is pressed.

share|improve this answer
    
I have a button on the page and not on the Component. I mean that I want to capture Component data as well as page data and save it. I'm not able to figure out how to do this... –  helpSFDC Oct 1 at 17:16
    
Hi @helpSFDC, please post a new question on stackexchange, possibly referring this one and we will try helping you. –  guy Oct 2 at 10:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.