Find your content:

Search form

You are here

How to render VisualForce components based on default values


I am trying to render one component based on whether another one has a specific value set on a Visualforce page.

Using the following, it works fine EXCEPT on the initial load (i.e. once I start toggling the driving question, the dependent question starts hiding/showing fine).

<apex:pageBlockSection id="myId">
    <apex:inputField value="{!My_Object__c.Question_1__c}">
        <apex:actionSupport event="onchange" reRender="myId"/>
    <apex:inputField value="{!My_Object__c.Dependent_Question__c}" rendered="{!My_Object__c.Question_1__c == 'Yes'}">

"Question_1__c" is a pick-list with the values Yes and No, defaulting to Yes.

I'm wondering if there is something going on where on the initial load the value is blank until I actually select the value (or save the page).

EDIT: based on discussion below, it looks like the field is initially set to NULL. However, checking for NULL as part of the rendered expression would also mean the field would show up when the picklist it is dependent on is set to the empty "--None--" value. The question becomes whether there is a way to differentiate the NULL of the initial page load vs the NULL being set manually by a user purely in Visualforce, or if a custom controller would be require to initialize the field (which would for all intents and purposes override the "default" value set in the configuration of the field).

Attribution to: Michael Welburn

Possible Suggestion/Solution #1

Edit: I see you mean to only have the specific sub question change. After extended comment discussion and also mention to @ebt the problem is that when not passed an id for the object the object is being created with NULL values rather than the defaults.

EDIT 2 Based on the edit in the question you should add a check to see if the Object ID is null as well as the field value.

Try changing your rendered statement to:

<apex:inputField value="{!My_Object__c.Dependent_Question__c}" rendered="{!My_Object__c.Question_1__c == 'Yes' || (ISNULL(My_Object__c.Question_1__c) && ISNULL(My_Object__c.Id))}">

I have tested this using the Case object as below:

<apex:page StandardController="Case">
<apex:pageBlockSection id="myId">
    <apex:inputField value="{!Case.Status}">
        <apex:actionSupport event="onchange" reRender="myId"/>
    <apex:inputField value="{!Case.Origin}" rendered="{!Case.Status=='New' || (ISNULL(Case.Status) && ISNULL(Case.Id))}"/>

Attribution to: Jon Hazan

Possible Suggestion/Solution #2

Boolean fields have three states, true, false, and null. It sounds like your picklist might be exhibiting the same issue.

Try prepopulating your object during the construction or action phase of the controller.


    public Class MyController{
         public object__c myObject__c {get;set;}
         public MyController(){
             myOjbect__c= new object__c(Question_1__c = 'Yes');

Attribution to: ebt
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content