Take the 2-minute tour ×
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.

What is View State in VisualForce page? I would like to know why we are using View state in VF page. Please help me .

share|improve this question

3 Answers 3

General

In general HTTP is a stateless protocol. Therefore there is no native way to implement a "state" in HTTP.

In salesforce it can be necessary to be aware of a specific state. Such a state could be and is in the standard case the value of a property in your controller.

Salesforce Example

Let's imagine you implement a simple form in visual force:

<apex:page controller="myController">
    <apex:form>
        <apex:commandButton value="do" action{!do}/>
    </apex:form>
</apex:page>

The corresponding controller looks like this:

public class myController
{
    public String abc;
    public myController()
    {
    abc = 'def';
    }

    void do()
    {

    }
}

Salesforce will execute the page as follows (simplified):

  1. Load visualforce page and detect the tag controller
  2. Your apex Class is called and the constructor is executed
  3. The variable abc is initialized by the constructor
  4. The value of your variable is stored in a "view state" field on your visual force page
  5. The page is sent to the client

Now the user is pressing the button "do"

  1. The browser starts a POST Request and sends data to salesforce
  2. Part of the request is your view state
  3. Salesforce "reads" your view state and reinitializes the variable abc without calling the constructor again.
  4. Salesforce executes the method "do"
share|improve this answer

There are many existing good and complete resources on what Visualforce view state is, how to use it, and how to manage it for good page performance.

I would suggest this intro page on the developer community site, which also has links for follow on reading for more detail.

share|improve this answer

The view state of a webpage is composed of all the data that's necessary to maintain the state of the controller during server requests (like sending or receiving date). So precisely view state -> implies the size of the page.

Whenever you use multiple forms in a VF page. Then typically the View state of the page exceeds its limit. The maximum limit should not exceed 135KB.

In order to reduce the view state, make use of the following:

A. "transient" keyword - to declare instance variables that can't be saved, and shouldn't be transmitted as part of the view state for a VF page.

for example :

transient DateTime theDate;
DateTime theOldDate;

Here , theDatecauses the transient date to be updated because it is being recreated each time the page is refreshed, whereas theOldDatecontinues to have its original value, which has been deserialized from the view state, so it remains the same.

B. Minimize the number of forms i.e <apex:form> on a page. Make use of <apex:actionRegion> instead of using 2 or more forms.

C. Refine your SOQL to retrieve only the data needed.

D. All public and private date members present in standard, custom and controller extensions are saved as a part of view state. so use transient variables which are not stored in state of page.

E. Instead of using <apex:commandlink> or <apex:commandbutton>, go for <apex:outputlink>

share|improve this answer

Your Answer

 
discard

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.