RSS Feeds

Saturday, May 15, 2010

ViewState Control in ASP.NET 4.0


View State is one of the most important and useful client side state management mechanisms. It can store the page value at the time of post back (Sending and Receiving information from Server) of your page. ASP.NET pages provide the View State property as a built-in structure for automatically storing values between multiple requests for the same page.

we generally used “EnableViewState” Properties for both Page Level and Server Control Level to maintain the view state. Till ASP.NET 3.5 Version, Page Level view state control treat as highest priorities. Which means If we set EnableViewState= “False” in page level that will automatically derived by all the server side control. In that case if we set “EnableViewState=”True”” for any server side control will treat as false, as we have defined them “False” in Page Level.Here is one complete article on ASP.NET 2.0/3.5 View State , which may helpful for you

Now, let’s have a look into the changes in ViewState Control in ASP.NET 4.0. There is a massive change in View State Control in ASP.NET 4.0 which is very much helpful for developer also. Asp.net 4.0 added a new property to Page object and server controls called ViewStateMode. ViewStateMode properties having 3 values

1. Enabled : This value will enable the view state for page level or control level. This is the default value for the Page object.

2. Disabled: This value will disable the viewstate for both page level and Control Level

3. Inherit : This value will make the control to inherit the setting of the parent. This is the default value for the server control.



These properties and their behaviors indicate the server control viewstates are totally independents on Page Level View State. Which means, ViewStateMode=”Disabled” will disabled the viewstate for all the control for that pages by default, but if we explicitly specify the page level control viewstatemode= ”Enabled”, then only that control viewstate will be maintained during the post back. Let's check few scenarios of ViewStateMode in ASP.NET 4.0

We have a sample asp.net web application and code snippet is given below.

[sourcecode language="html"]
<%@ Page Language="C#" ViewStateMode="Disabled" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="ViewStateDemoLebel1" ViewStateMode="Enabled" runat="server"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>
</form>
</body>
</html>
[/sourcecode]

In Code Behind :

[sourcecode language="csharp"]
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ViewStateDemoLebel1.Text = "Default Value of Control";
}
}
[/sourcecode]

Now, let's explore the scenarios of ViewStateMode

Scenario 1 : Both Page and Control Level ViewStateMode=”Disabled”


[sourcecode language="html"]
<%@ Page Language="C#" ViewStateMode="Disabled" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Label ID="ViewStateDemoLebel1" ViewStateMode="Disbaled" runat="server"></asp:Label>
[/sourcecode]

If we have ViewStateMode Disabled for both the page and control level and we run the program, first time label text will be “Default Value of Control” . But After Click on Button1, means after postback “ViewStateDemoLebel” value will be Blank. Because we have disabled the viewstate mode properties for both control and page level.


Scenario 2 : Page Level ViewStateMode=”Disabled” and Control Level ViewStateMode=”Enabled”


[sourcecode language="html"]
<%@ Page Language="C#" ViewStateMode="Disabled" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Label ID="ViewStateDemoLebel1" ViewStateMode="Enabled" runat="server"></asp:Label>
[/sourcecode]

In this case, after PostBack ViewStateDemoLebel1 value will be the “Default Value of Control” as ViewStateMode set to Enabled. So, in this point we can understnad that page level viewstatemode is totally independent on the server control level ViewStateMode.


Scenario 3 : Page Level ViewStateMode=”Enabled” and Control Level ViewStateMode=”Disabled”


To demonstrate the scenario, I have added one more label control named ViewStateDemoLebel2 in aspx page and default value of that control is "Default Value of Control 2”. Below is the code snippet for that aspx page.

[sourcecode language="html"]
<asp:Label ID="ViewStateDemoLebel1" ViewStateMode="Disabled" runat="server"></asp:Label>
<asp:Label ID="ViewStateDemoLebel2" runat="server"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
[/sourcecode]

Here, PageLevel ViewStateMode is enabled, and we explicitly defined ViewStateMode="Disabled" for ViewStateDemoLebel1. so as a result, after post back we have the value of control 2.


Scenario 4 : Page Level ViewStateMode=”Enabled” and Control Level ViewStateMode=”Inherited”


[sourcecode language="html"]
<%@ Page Language="C#" ViewStateMode="Enabled" AutoEventWreup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Label ID="ViewStateDemoLebel1" ViewStateMode="Inherite" ViewStateMode="Enabled" runat="server"></asp:Label>
[/sourcecode]

In this case, child control automatically ineherite the properties of page level. So for ViewStateDemoLebel1 control viewstatemode will be “enabled” as it used “Inherit” properties and Page Level ViewStateMode Set to enabled.


Summary :


In this article we have seen How ViewStateMode properties can be used in ASP.NET 4.0 to take a better control on PageLevel and control Level View State. I have explained most of the scenarios of ViewStateMode. Hope you have enjoyed the article. Cheers!

kick it on DotNetKicks.com
Shout it

19 comments:

DotNetShoutout said...

ViewState Control in ASP.NET 4.0 « Abhijit's World of .NET...

Thank you for submitting this cool story - Trackback from DotNetShoutout...

Abhishek Sur said...

Thats good one. I think I have seen a video on the same long ago.

Cool to disable Viewstate completely using ViewStateMode.

Cheers.

Which Free Web Hosting – Supports Customs COM Components for ASP ? | Web Hosting and Web Design For Small Business said...

[...] ViewState Control in ASP.NET 4.0 « Abhijit's World of .NET [...]

Brij said...

Good One Dude!!!

This feature is really nice and gives us more power over viewstate...

Cheers,
Brij

Abhijit Jana said...

Thanks Brij !!!

Abhijit Jana said...

Thanks !

What is the best free web hosting that you recommend? | Web Hosting and Web Design For Small Business said...

[...] ViewState Control in ASP.NET 4.0 « Abhijit's World of .NET [...]

Does anyone know of a webhosting company that supports asp.net? | Web Hosting and Web Design For Small Business said...

[...] ViewState Control in ASP.NET 4.0 « Abhijit's World of .NET [...]

Why am I getting a runtime error on my ASP.NET application -only- on the web server? | Web Hosting and Web Design For Small Business said...

[...] ViewState Control in ASP.NET 4.0 « Abhijit's World of .NET [...]

How can i found out what type of server my webpage is hosted on? | Web Hosting and Web Design For Small Business said...

[...] ViewState Control in ASP.NET 4.0 « Abhijit's World of .NET [...]

Is asp.net a common web file? | Web Hosting and Web Design For Small Business said...

[...] ViewState Control in ASP.NET 4.0 « Abhijit's World of .NET [...]

Thirmal Reddy said...

Superb Article before i don't know exact what is View state ThanQ Abhijit

Abhijit Jana said...

Thanks. Its really great features in ASP.NET 4.0 .

Christian Kerr | citizens automobile finance inc official site | Automobile Platform Coachwork Design said...

[...] ViewState Control in ASP.NET 4.0 « Abhijit's World of .NET [...]

Car Leasing Specials: Top Car Choices for College Kids | Portable Car Seat said...

[...] ViewState Control &#1110&#1495 ASP.NET 4.0 « Abhijit's World &#959f .NET [...]

A great collection of asp.net books ( updated daily ) – Lazydesis | VB WebDev Insider said...

[...] ViewState Control in ASP.NET 4.0 « Abhijit's World of .NET [...]

What are some good ways to cure or control snoring? | How To Sleep Better Blog said...

[...] ViewState Control in ASP.NET 4.0 « Abhijit's World of .NET [...]

server side asp | SERVERS said...

[...] ViewState Control in ASP.NET 4.0 [...]

402 said...

fBJLnJ knntosaewqfw, [url=http://emwsdavvizea.com/]emwsdavvizea[/url], [link=http://vpnaciznvgsq.com/]vpnaciznvgsq[/link], http://jewzwytwwaxm.com/

Post a Comment