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
PASHPASH 

How will you display a custom object field using custom controller

I have a custom object. And I have a field Name and ID.. How can I display these fileds in Visual Force page, using custom controller.
Thank you in advance.
Best Answer chosen by PASH
PeaceMakerPeaceMaker
Use this below sample , Mark this as solution if it helps you

Sample Apex class

public with sharing class customController {
public list<customobject__c> objlist {get;set;}
Public VFC_customController(){
objlist = [Select Id, Name from customobject__c];  // Use where condition if you want
}

Sample Visualforce Page

<apex:page Controller="customController">
<apex:form>
<apex:pageBlock>
<apex:pageBlockSection>
<apex:pageBlockTable Id="allcon" value="{!objlist}" var="ans" rendered="{!If(objlist != null,True,False)}" width="100%">
<apex:column ><apex:facet name="header">id</apex:facet>
                 <apex:outputfield value="{!ans.id}"></apex:outputfield>
                   </apex:column>
                <apex:column > <apex:facet name="header">Name</apex:facet>
                 <apex:outputLink Value="/{!ans.Id}">
                <apex:outputfield value="{!ans.Name}"></apex:outputfield></apex:outputLink>
                 </apex:column>
                </apex:pageBlockTable>
                </apex:pageBlockSection>
</apex:pageBlock>
</apex:form >
</apex:page>

All Answers

PeaceMakerPeaceMaker
Use this below sample , Mark this as solution if it helps you

Sample Apex class

public with sharing class customController {
public list<customobject__c> objlist {get;set;}
Public VFC_customController(){
objlist = [Select Id, Name from customobject__c];  // Use where condition if you want
}

Sample Visualforce Page

<apex:page Controller="customController">
<apex:form>
<apex:pageBlock>
<apex:pageBlockSection>
<apex:pageBlockTable Id="allcon" value="{!objlist}" var="ans" rendered="{!If(objlist != null,True,False)}" width="100%">
<apex:column ><apex:facet name="header">id</apex:facet>
                 <apex:outputfield value="{!ans.id}"></apex:outputfield>
                   </apex:column>
                <apex:column > <apex:facet name="header">Name</apex:facet>
                 <apex:outputLink Value="/{!ans.Id}">
                <apex:outputfield value="{!ans.Name}"></apex:outputfield></apex:outputLink>
                 </apex:column>
                </apex:pageBlockTable>
                </apex:pageBlockSection>
</apex:pageBlock>
</apex:form >
</apex:page>
This was selected as the best answer
PASHPASH
Thank you so much. Grateful to you.
PeaceMakerPeaceMaker
Welcome, Mark this  as solution if it solves your problem.
PASHPASH
Sure. I will run it and mark it. Thnx again
Lalitha SelvarajLalitha Selvaraj
I get error on this line .. 
Public VFC_customController() : Invalid constructor name
Lalitha SelvarajLalitha Selvaraj
worked fineNo issues
Suraj DemgundeSuraj Demgunde
I get error on this line .. 
Public VFC_customController() : Invalid constructor name