How to fetch data from Controller and display it in Visualforce page?

How to fetch data from Controller and display it in Visualforce page?

Sample code:

Visualforce page:

<apex:page sidebar="false" controller="Sample">
    <apex:form >
       <apex:pageblock id="account" title="Account Details(Standard Object)" >
            <apex:pageblockTable value="{!acc}" var="a">
                <apex:column value="{!a.Name}"/>
                <apex:column value="{!a.AccountNumber}"/>
            </apex:pageblockTable>
            <apex:pageblockButtons >
                <apex:commandButton value="Fetch Account" action="{!fetchAccount}" reRender="account"/>
            </apex:pageblockButtons>
        </apex:pageblock>
       <apex:pageblock id="member" title="Member Details(Custom Object)">
            <apex:pageblockTable value="{!mem}" var="m">
                <apex:column value="{!m.Name}"/>
            </apex:pageblockTable>
            <apex:pageblockButtons >
                <apex:commandButton value="Fetch Member" action="{!fetchMember}" reRender="member"/>
            </apex:pageblockButtons>           
        </apex:pageblock>           
    </apex:form>
</apex:page>

Apex Controller:

public with Sharing class Sample
{
    public Integer temp{get;set;}
    public Date d;
   
    public List<Account> acc {get;set;}
    public List<Member__c> mem {get;set;}   
      
    public void fetchAccount()
    {
        acc = [SELECT Name, AccountNumber FROM Account];
    }
   
    public void fetchMember()
    {
        mem = [SELECT Name FROM Member__c];
    }
}

Output:

Leave a Reply