Monday, May 26, 2014

ENABLE VIEW STATE AND VIEW STATE MODE

With ASP.Net 4.0,
A new property was introduced
ViewStateMode
Now both of these properties are available
1 ViewStateMode
2 Enable View State
ViewStateMode
This property allows you to Disable View State at parent level and Enable it selectively at child level.
Example:
 If a form contains Parent Controls and child controls. So we can disable view state for parent level, and enable view state for child controls

EnableViewState:
EnableViewState property does not allow this. Simple.




 .cs page


Now, if we run the application,


Post Back (button click):
Now if we click on the Button, page will be posted back and the result will be the same
Label is showing date time, but the date and time is not current.



Now let’s disable View State at the page level by using the following property
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.
 
 
First request after disabling View State at page level (ViewStateMode = Disabled)
 



Post Back:




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 State at label, we would expect the date time to be displayed during post back.


 Here is the trace file


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

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

Now, again I am modifying the form 
 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default"  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" CodeFile="Default.aspx.cs" Inherits="_Default"  EnableViewState="false" Trace="true"  %>

As we see, the View State size is  0  for label.


  
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

No comments:

Post a Comment

Thank you for visiting my blog

Kubernetes

Prerequisites We assume anyone who wants to understand Kubernetes should have an understating of how the Docker works, how the Docker images...