View State?

Techieshravan
Posted by in ASP.NET category on for Beginner level | Views : 4323 red flag
Rating: 5 out of 5  
 1 vote(s)

How the ViewState is maintained by ASP.NET?

HTTP protocol is stateless protocol. Each time you request a web page from the web application, from web application perspective, you are a completely new person.

HTTP protocol can't remember the data of a request once it is submitted to the web server so we can't use the request data of one request in an other request.

How ASP.NET manages the transcend behavior of the HTTP?

Look at the below example:

<%@ Page Language="C#"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void btnSubmit_Click(object sender, EventArgs e)
{
TextBox1.Text = (Int32.Parse(TextBox1.Text) + 1).ToString();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>ViewState</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" onclick="btnSubmit_Click" Text="Submit" Height="35px" Width="95px"/>
<asp:TextBox ID="TextBox1" Text="2" runat="server" Height="29px" Width="168px"/>
</div>
</form>
</body>
</html>

In the above example i have assigned a value 2 to the TextBox Control .Each time i click on the button control, the value displayed in TextBox Control is incremented by one.

As our protocol is stateless every time the value of the control should remain same but it is incremented by1. How does the TextBox control preserves its value across the post-backs to the server.


The ASP.NET Framework uses a technique called View State.

In the browser go to View Menu-->Source, In the page we can find a hidden form field named _VIEWSTATE that looks like this.


<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTM0MDE0NzQwOGRk8M8lz6HBakvRx17SX5/m1fVkRvA=" />


This hidden form field contains the value of TextBox control's Text property & the values of any other control properties that are stored in View State.

When the page is posted back to the server, the ASP.NET excludes this string and recreates the values of the all the properties stored in View State. In this way ASP.NET preserves the state of the control properties across the postbacks to the web server.

By default View State is enabled for every control in the ASP.NET. View State is good thing but some times become very large by stuffing too much data into View State can slowdown the rendering of a page. Because the contents of hidden form field must be traveled between web server and web browser.

How to disable the View State?

Every ASP.NET control has a property named "EnableViewState". If you set the property value to false, then View State is disabled for the control.

Page copy protected against web site content infringement by Copyscape

About the Author

Techieshravan
Full Name: SHRAVAN KUMAR
Member Level: Starter
Member Status: Member
Member Since: 12/20/2009 2:22:44 PM
Country: India

http://blogs.windowsclient.net/techieshravan

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)