function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

how to pass values from VisualForcePage controller to Component Controller


Can u please explain how to pass textbox value from Visual force page to Component with in VF Page.

i had a VF Page in that i had 4 textboxes of InputFields and one apex Component control.
when user enter value in first textbox of VFPage then i have to display a datatable in the Component control in that Page.

so how can i pass value from VFPage to Component controller.
The KnightThe Knight
If there is a common custom controller for both the component and VF page it is possible. Once u enter the values in the input fields collect it in a list with getter and setter method which you will be accessing in the component.



We can achieve this functionality using attributes tags in the component.

Every attribute defined in the component will have its value initialized form the VF page field variables or the controller variables.

Inside my code snippet, I have created a component which shows the name of the Account and associated related list of Contacts.

Name of the Account and Contact [] are passed from VF to the component.

Below are the code snippets of Component, VF Page and Controller.



COMPONENT: <apex:component > <apex:attribute name="accountName" description="Name of Account Entered By User" type="String" required="true"/> <apex:attribute name="contactList" description="Contains List of Contacts" type="Contact[]" required="true"/> <apex:outputText value="{!accountName}"></apex:outputText> <apex:pageblock > <apex:pageBlockTable value="{!contactList}" var="contact"> <apex:column value="{!contact.FirstName}"/> <apex:column value="{!contact.LastName}"/> <apex:column value="{!contact.Department}"/> <apex:column value="{!contact.Email}"/> <apex:column value="{!contact.Title}"/> </apex:pageBlockTable> </apex:pageblock> </apex:component> VISUALFORCE: <apex:page controller="MyCompController"> <apex:pageBlock id="compBlock" title="Related List of Contacts"> <c:MyComponent accountName="{!accountName}" contactList="{!contactList}"/> </apex:pageBlock> <apex:pageBlock > <apex:form > <apex:outputLabel >Enter Account Name</apex:outputLabel> <apex:inputText value="{!accountName}"/> <apex:commandButton action="{!fetchAccountId}" value="Click!" reRender="compBlock"/> </apex:form> </apex:pageBlock> </apex:page> APEX_CONTROLLER: public class MyCompController{ String accountName; String accountId; Contact[] contactList; public MyCompController(){ accountName = 'Edge Communications'; } public void setAccountName(String accountName){ this.accountName = accountName; } public String getAccountName(){ return accountName; } public void setAccountId(String accountId){ this.accountId= accountId; } public String getAccountId(){ return accountId; } public Contact[] getContactList(){ return contactList; } public PageReference fetchAccountId(){ Account acc = [Select Id from Account where Name = :accountName ]; if(acc != null){ accountId = acc.Id; } contactList = [Select c.Department, c.FirstName, c.LastName, c.Email, c.Title from Contact c where AccountId = :accountId]; return null; } }