6

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.

5

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);   
    }

}
  • +1 Thanks. This does work, but I'm wondering if there is an approach that would be better suited for dynamic controls. With dynamic controls I could have any number of controls across a range of data types. It isn't practical to have hard coded properties for them to bind to. I guess I'm looking for dynamic binding properties to do with the dynamic controls. – Daniel Ballinger Apr 23 '13 at 21:29
  • @DanielBallinger what do you mean by dynamic controls? Would you please elaborate? – doga Apr 24 '13 at 4:32
  • 1
    Say you were calling a web service to determine which visualforce components to add to the page. You might have several InputText and a range of other controls based on the web service results. As these controls can vary dynamically in Apex, how do you get equally dynamic properties to bind to? – Daniel Ballinger Apr 26 '13 at 3:01
  • I've accepted this answer. In the absence of another example, I think the only way to get a value from a dynamic control is to bind it to something. – Daniel Ballinger May 9 '13 at 1:59
  • 1
    Hi, i know this is almost an year old post, but i have a similar kind of a requirement. I am tying to achieve pagination using command buttons as dynamic components. but i want to return the value of the button number to my controller. Can anyone please help me? – Sam Mar 11 '14 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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