function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
PzyrenPzyren 

Get Checkbox state in controller Apex | Visualforce

Hello all, I have a checkbox in my visualforce page just like this :

<apex:inputCheckbox />

 I want to be able in my controller, to see whether or not the checkbox is checked. I am making a query that is based on the checkbox's state. For example. This is the state if the checkbox is not selected.

 

select name from foo__c where id = contactid 

 if the checkbox is selected, I want my query to be something like this

 

select name from foo__c where id IN :allContactIds

I'm stuck on this. Any help?

SaraagSaraag

Assuming that users click a button after checking the box.

 

<apex:inputcheckbox value="{!variable1}"

 

Here variable1 references controller class variable. After user clicks the button, in the action method you would check for if(variable1)

 query1

else

  query2

 

Saraag

KunalSharmaKunalSharma

you can use action support tag and in the controller method you can check if the value is true or false. PFB sample code:

 

<apex:inputCheckbox>
<apex:actionSupport event="onchange" action="{! controllerAction}"  reRender="component"/>
</apex:inputCheckbox>