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
Raju Sudheer 3Raju Sudheer 3 

what is view state.? can any one explain clearly with example

Prabhat Kumar12Prabhat Kumar12
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):

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

Now the user is pressing the button "do"

The browser starts a POST Request and sends data to salesforce
Part of the request is your view state
Salesforce "reads" your view state and reinitializes the variable abc without calling the constructor again.
Salesforce executes the method "do"

Beautifully answered by http://salesforce.stackexchange.com/users/6667/christian-deckert


 
William TranWilliam Tran

Raju,

In simple terms:  ViewState store the "state" of your page so you don't lose everything.

For example, when you have a shopping cart and trying to pay but enter the wrong credit card number.

With ViewState, you still have your cart with an error message.

Without Viewstate, all you items in the shopping cart are lost and you have to start over again.

For screens that require user inputs, Viewstate is needed to remember that info until the user is done with the whole process.


thx.

Here's a good intro:

https://developer.salesforce.com/page/An_Introduction_to_Visualforce_View_State