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 ASP.net 4.0)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="foo.aspx.cs" Inherits="WebApplication4.DemoPages.foo" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</form>
</body>
</html>
At Page Load, I am setting label’s Text property to current date time stamp.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
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="WebApplication4.DemoPages.foo"
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="WebApplication4.DemoPages.foo"
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="WebApplication4.DemoPages.foo"
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!
Cheers!
Comments
- Anonymous
May 22, 2013
great way of explaining......helped me a lot...Thank You... - Anonymous
June 12, 2013
d - Anonymous
August 21, 2013
great works it clear all my doubts. - Anonymous
August 26, 2013
Nice explanation.. - Anonymous
September 24, 2013
cool.. its really helpfull - Anonymous
October 24, 2013
Really helpfull.. Thanks - Anonymous
December 09, 2013
The comment has been removed - Anonymous
January 26, 2014
The comment has been removed - Anonymous
April 01, 2014
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... - Anonymous
April 15, 2014
The comment has been removed - Anonymous
May 02, 2014
Really Nice one, clear lots of things - Anonymous
May 20, 2014
great way of explaining...thanks alot... - Anonymous
September 08, 2014
Thanks for the post u cleared my doubt - Anonymous
September 25, 2014
Thanks it's really helpful for all of us. - Anonymous
May 12, 2015
Clear my all confusion about view state. Thank u so much