+ Start a Discussion

rerender component without loosing data

Hi All, 


I am trying to render an inputField if the user selects the field 'Other' in a dropdownlist in visualforce page. Everything works fine, but the only thing is it clears the data the user enters.  I know I can archieve this by putting a wrapper component, but I want to know if there is a way to do this without doing that. 


I have the following code:

<apex:pageBlock id="pb1">
            <apex:pageblockSection collapsible="false" columns="2" id="pbs1">
                <apex:panelGrid columns="2">
                    <apex:outputLabel value="{!$Label.City}" for="city"/>
                    <apex:inputField id="city" value="{!lead.City__c}" required="true"/>
                    <apex:outputLabel value="{!$Label.Country}" for="country"/>
                    <apex:actionRegion >
                        <apex:inputField id="country" value="{!lead.Country__c}" required="true">
                             <apex:actionSupport event="onchange" rerender="pbs1" />
                    <apex:outputLabel value="{!$Label.PrimaryReason}" for="primaryReason" />
                        <apex:inputField id="primaryReason" value="{!lead.Primary_Reason__c}">
                            <apex:actionSupport event="onchange" rerender="pbs1" />
                    <apex:outputLabel value="If Other Reason is selected enter more information here" for="primaryReasonOther"
                                        rendered="{!IF(lead.Primary_Reason__c == 'Other', 'true', 'false')}"/>
                    <apex:inputField id="primaryReasonOther" value="{!lead.Other_Reason__c}"
                             required="true" rendered="{!IF(lead.Primary_Reason__c == 'Other', 'true', 'false')}"/>
            <apex:pageBlockSection collapsible="false" columns="1" id="pbs3">
                <apex:panelGrid columns="2" rendered="{!IF(lead.Country__c == 'United States', 'true', 'false')}">
                    <apex:outputLabel value="{!$Label.Diversity_Programs}" for="diversityprogramsusonly" />
                    <apex:inputField id="diversityprogramsusonly" value="{!lead.Diversity_Program_US_Only__c}" />


If you see above, I am doing it both ways, but I am trying to test out if I can archieve it without adding a wrapper around the component. When I do that it works, but it clears all the data. Does anyone know why the data is not being stored to the lead object? Also, if there is a solution for this. I am looking for something other than a wrapper component.  Thanks.




This is because your dropdown list is in its own actionregion, which means only that value will be submitted back to the controller when the action support fires, not the value entered in the input field.  If you then rerender the input field that will be updated with the controller's view of the contents, which will be empty as the value entered by the user was not submitted back. 


If you include the input field inside the action region with the dropdown then the user's entered data will be posted back to the controller.