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!


Comments (15)
  1. jay says:

    great way of explaining……helped me a lot…Thank You…

  2. soubhagya says:

    great works it clear all my doubts.

  3. Sandhya says:

    Nice explanation..

  4. pravin says:

    cool.. its really helpfull

  5. Pushpendra says:

    Really helpfull.. Thanks

  6. Pawan Moudgil. says:

    Not working fine when having with Textbox.

    if you do same with asp:textbox then viewstate value still maintain and set value again to text box weather you use ViewStateMode="Disabled" or EnableViewState="False" in both "page level" and "control level."

    why so?

  7. Ashish Goyal says:

    @Pawan: Text box is one of those controls which uses PostBack data and is not really dependent on the viewstate to hold the text. Such controls cannot be used when understanding/experimenting with ViewState concepts.

  8. Leo says:

    Pawan Moudgil – same confusion here – I disabled the viewstatemode in master, enabled in web form, saw that everything was working, on a page with textboxes, but then going to a page with a dropdownlist, it didn't – now I understand that these two possibly act differently…

  9. Bob Elander says:

    Golden beans, thanks for this post it really cleared my head about it.  

  10. Sanjay Modi says:

    Really Nice one, clear lots of things

  11. Roshma says:

    great way of explaining…thanks alot…

  12. Dev says:

    Thanks for the post u cleared my doubt

  13. Mayur says:

    Thanks it's really helpful for all of us.

  14. Aamir says:

    Clear my all confusion about view state. Thank u so much

Comments are closed.

Skip to main content