+ Start a Discussion

What's the best practice for vew and edit modes for a VF page?

I'm creating a new VF page to replace the standard SF view and edit layouts.  I overrode the new, edit and view buttons on the object to all point to my new VF page.

Regardless of whether you click the edit or view links the form always comes up in Edit format.  This isn't surprising since my fields all use the <apex:inputField> tag.  This leads me to my question.


What's the best practice for supporting both edit and view modes for a VF page? 


I could create two VF pages, one for editing (with <apex:inputField> tags) and one for viewing (with <apex:outputField> tags) but that seems like a less than ideal solution.


Are there other options?




Having two VF pages backed by a single controller isn't that bad a thing, IMO. One downside is that it makes it easier for developers to forget to modify both edit and view mode pages when, e.g., adding a field.


You can have both modes in a single page by using the "rendered" attribute. You would have a method on your controller like "getViewMode()" which returns true in View, false in Edit. Then your code would look something like:



<apex:pageBlock title="View Object" rendered="{!viewMode = true}"> </apex:pageBlock> <apex:pageBlock title="Edit Object" rendered="{!viewMode = false}"> </apex:pageBlock>



Jeremy Kraybill

Austin, TX