Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is it possible to add a CSS class to one of the Visualforce page objects from controller? What I want to achieve is to handle one exception in controller. From catch block I want to be able to hide a form (add a CSS class to form with display:hidden).

share|improve this question
up vote 4 down vote accepted

Create a variable in your controller,

public boolean isVisible{gt;set;}

assign it true/false depending on condition(exception in your case)

surround form you want to show by an output panel

<apex:outputPanel rendered="{!isVisible}">
share|improve this answer

You can assign a class name to the variable and then dynamically assign a new value:

Apex Class:

public with sharing class MyController{

    public String myCSSClass {get; set;}

    public MyController(){
        myCSSClass = 'visible';

    public PageReference myMethod{
            myCSSClass = 'hidden';

            return null;

Visualforce page: after user clicks on the "Check" button a method "myMethod" called. If it fails the variable gets a "hidden" class.


<apex:commandButton action="{!myMethod}" value="Check" reRender="myPanel">

<apex:outputPanel id="myPanel" styleClass="{!myCSSClass}">
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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