[SalesForce] Dynamic Visualforce Components post back values

If I add an Component.Apex.InputText to a Visualforce page using dynamic Visualforce components how can I see the value that is entered in that component when the user submits the page back via a command button?

Page snippet:

<apex:dynamicComponent componentValue="{!dynamicPropertyControls}"/>

Controller snippet:

public Component.Apex.OutputPanel getDynamicPropertyControls() {
    Component.Apex.OutputPanel dynOutPanel= new Component.Apex.OutputPanel();

    Component.Apex.InputText textValue = new Component.Apex.InputText();
    textValue.value = 'Test';
    dynOutPanel.childComponents.add(textValue);

    return dynOutPanel;
}

So the user will see a text input with the value 'Test'. If they change the value and post the page back via a command button how can I get the changed value?

Do I need to maintain a list of dynamic controls that were added to the page in a member variable so they come back via view state?

I suspect it will probably be easier to completely change my approach to avoid the dynamic components and instead bind to a collection of custom objects. Still, how would you ever get the value back from a dynamic control, especially if it isn't bound to anything.

Best Answer

I tried following approach and it works for me. VF Page:

<apex:page controller="DynaCompCtrl">
<apex:form>
    <apex:dynamicComponent componentValue="{!DynamicPropertyControls}"/>
    <apex:commandButton value="Save" action="{!save}"/>
</apex:form>

Controller:

public class DynaCompCtrl{  
    public String myval {get;set;}

    public Component.Apex.OutputPanel getDynamicPropertyControls() {
       Component.Apex.OutputPanel dynOutPanel= new Component.Apex.OutputPanel();

       Component.Apex.InputText textValue = new Component.Apex.InputText();
       textValue.value = 'Test';
       textValue.expressions.value = '{!myval}';
       dynOutPanel.childComponents.add(textValue);

       return dynOutPanel;
    }    

    public void save() {
     system.debug('#########-'+myval);   
    }

}
Related Topic