Introducing DotNetFunda.com on mobile http://m.dotnetfunda.com ! Be with DotNetFunda.com on the go !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 3306 |  Welcome, Guest!   Register  Login
Home > Articles > ASP.NET > View State?

View State?

Article posted by Techieshravan on 12/30/2009 | Views: 2125 | Category: ASP.NET | Level: Beginner red flag


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.

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Latest Articles from Techieshravan
Experience:2 year(s)
Home page:http://blogs.windowsclient.net/techieshravan
Member since:Sunday, December 20, 2009
Level:Starter
Status: [Member]
Biography:
>> Write Response - Respond to this post and get points
Related Posts

In this article, we are going to learn how to implement SQL Caching in ASP.NET using Poll based SQL Cache dependency. The push based SQL Cache dependency shall be covered in other article. In poll based, SQL Cache dependency checks for the updated data after every specified duration. With the help of Caching, we can retain pages or data across HTTP requests, so that we can reuse them without recreating them.

In this section we will learn about the basics of MVC and then see how we can implement the same in ASP.NET using HttpHandlers.

In this Article, we will learn how to display Loading.. message while uploading any files.

This article describes how to save an image file into the database and show them in the GridView.

From this Article you can know how make Paging for First, Next, Previous and Last in GridView.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/21/2012 7:34:53 AM