ASP.Net View State: EnableViewState vs ViewStateMode

With ASP.Net 4.0, a new property for View State was introduced, it was called ViewStateMode. Prior to ASP.Net 4.0, we used to use EnableViewState property. Now for an ASP.Net 4.0 application, both of these properties are available. So the question comes in which one we should use? And how are they different?

To answer the first question, you can use whichever you want, if it suits you.

Now lets see what the difference between these two.


ViewStateMode property allows you to Disable View State at parent level and Enable it selectively at child level.

          EnableViewState property does not allow this. Simple.

Both of these properties allow you to Enable view state at parent level and Disable it at child level.


Lets learn by example. This is how my foo.aspx page looks (using 4.0)


 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="foo.aspx.cs" Inherits="" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        <asp:Button ID="Button1" runat="server" Text="Button" />

At Page Load, I am setting label’s Text property to current date time stamp.

         protected void Page_Load(object sender, EventArgs e)
                Label1.Text = DateTime.Now.ToString();

Now, if we run the application, this is how the foo.aspx page will look on browser.

First Request:




Post Back (button click):



So the label is showing the date time stamp as expected. Now if we click on the Button, page will be posted back and the result will be the same. Label is still showing the date time stamp but that is not the current time, instead, it is the time of the first request. Which means, the label text is getting populated from ViewState.

Now lets disable View State at the page level. Lets use ViewStateMode property. This is how foo.aspx page directive looks like.

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="foo.aspx.cs" Inherits="" 
 ViewStateMode="Disabled" %>

Now, if I re-run the application, first time when the page loads, label will show current date time (which is expected). And on post back (clicking the button), we will not see the date time stamp any more. Reason? ViewState is disabled now.

First request after disabling View State at page level (ViewStateMode = Disabled)


Post Back:



So far so good. Till this point, there is almost no difference between the two properties ViewStateMode and EnableViewState. Now what if I want to enable view state, but only for the label. This is what we do.

 <asp:Label ID="Label1" runat="server" Text="Label" ViewStateMode="Enabled"></asp:Label>

Now lets run the application once again.

Since we have enabled View Sstate at label, we would expect the date time stamp to persist during post back.

First request: At page level –> ViewStateMode=Disabled; at control level(label) –> ViewStateMode=Enabled



Post back



THIS is what you could not do using EnableViewState property. If you try to use EnableViewState, once view state is disabled at page you can not selectively enable it at controls. Lets try it

I have changed my foo.aspx and to use EnableViewState now.

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="foo.aspx.cs" Inherits="" 
 EnableViewState="false" %>
 And at label:
 <asp:Label ID="Label1" runat="server" Text="Label" EnableViewState="true" ></asp:Label>

So lets run the application.

First Request: at page level –> EnableViewState=False; at control (label) –> EnableViewState=True 



Post back:



So since the date time stamp does not persists, it indicates that View State is not enabled for label control. We can confirm this by enabling Tracing

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="foo.aspx.cs" Inherits="" 
 EnableViewState="false" Trace="true" %>
 This is relevant trace result for the post back



As we see, the ViewState size is 0 for label.

Now a note of caution: While using EnableViewState, we can still Enable view state at parent level and Disable it at control level.

To summarize:

Using ViewStateMode: Disable view state at Parent, Enable it at child level –> Works well

                         Enable view state at Parent, Disable it at child level –> Works well

Using EnableViewState: Disable view state at parent, Enable it at child –> Does not work

                           Enable view state at parent, Disable it at child –> Works well


Hope this helps!