Thursday, 2 February 2012

View State | ASP.Net Tutorial | ASP.Net Tutorial PDF

   ASP.NET controls automatically retain their data when a page is sent to the server in response to an event (such as a user clicking a button). Microsoft calls this persistence of data view state. In the past, developers would have had to resort to hacks to have the application remember the item a user had selected in a drop-down menu, or store the content entered into a text box; typically, these hacks would have relied on hidden form fields.
   This is no longer the case. Once they’re submitted to the server for processing, ASP.NET pages automatically retain all the information contained in text boxes and drop-down lists, as well as radio button and checkbox selections. They even keep track of dynamically generated tags, controls, and text. Consider the following code written in the “ancient” ASP (not ASP.NET!) framework:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Sample Page using VBScript</title>
</head>
<body>
<form method="post" action="sample.asp">
<input type="text" name="nameTextBox"/>
<input type="submit" name="submitButton" value="Click Me" />
<%
If Request.Form("nameTextBox") <> "" Then
Response.Write(Request.Form("nameTextBox"))
End If
%>
</form>
</body>
</html>
         Loading this page through an ASP-enabled web server (such as IIS) would reveal that the view state is not automatically preserved. When the user submits the form, the information that was typed into the text box is cleared, although it’s still available in the Request.Form("nameTextBox") property. The equivalent page in ASP.NET demonstrates this data persistence using view state:
Visual Basic               LearningASP\VB\ViewState.aspx
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Sub Click(ByVal s As Object, ByVal e As EventArgs)
messageLabel.Text = nameTextBox.Text
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>View State Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="nameTextBox" runat="server" />
<asp:Button ID="submitButton" runat="server"
Build Your Own ASP.NET 3.5 Web Site Using C# & VB (www.sitepoint.com)
ASP.NET Basics 39
Text="Click Me" OnClick="Click" />
<asp:Label ID="messageLabel" runat="server" />
</div>
</form>
</body>
</html>

C#                          LearningASP\CS\ViewState.aspx
<%@ 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">
void Click(Object s, EventArgs e)
{
messageLabel.Text = nameTextBox.Text;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>View State Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox id="nameTextBox" runat="server" />
<asp:Button id="submitButton" runat="server"
Text="Click Me" OnClick="Click" />
<asp:Label id="messageLabel" runat="server" />
</div>
</form>
</body>
</html>
       In this case, the code uses ASP.NET controls with the runat="server" attribute. As you can see in below Figure, the text from the box appears on the page when the button is clicked, but also notice that the data remains in the text box! The data in this example is preserved by view state.

          Figure : ASP.NET maintaining the state of the controls
      You can see the benefits of view state already. But where’s all that information stored?
ASP.NET pages maintain view state by encrypting the data within a hidden form field. View the source of the page after you’ve submitted the form, and look for the following code:
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"
value="/wEPDwUKLTEwNDY1Nzg0MQ9…0fMCR+FN5P6v5pkTQwNEl5xhBk" />

       This is a standard HTML hidden form field. All information that’s relevant to the view state of the page is stored within this hidden form field as an encrypted string.
        View state is enabled for every page by default. If you don’t intend to use view state, you can turn it off, which will result in a slight performance gain in your pages. To do this, set the EnableViewState property of the Page directive to false:
<%@ Page EnableViewState="False" %>