O'Reilly logo

ASP.NET 4 24-Hour Trainer by Toi B. Wright

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 10A. View State in Web Forms

View state is used to preserve the state of controls on a Web Form across postbacks. In this lesson I show you how view state is stored on a web page, why it is useful, and how to turn it off.

VIEWING VIEW STATE

This is the code for a simple web page that has only one server control on it:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Label.aspx.cs"
    Inherits="Lesson10a.Label" %>

<!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="Label1" runat="server" Text="Label" />
    </div>
    </form>
</body>
</html>

When the preceding page is rendered on a browser, this is the HTML source for the page:

<!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><title>

</title></head>
<body>
    <form method="post" action="Label.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__viewstate"
   value="/wEPDwUJOTczNTMyNjI5ZGTL47vwelS4H7Q78eBnfVHkSleBV1ii753zaCBSWQAKKw==" />
</div>

     <div>
        <span id="label1">Label</span>
    </div>
    </form>
</body>
</html>

The view state is stored in the hidden field named __VIEWSTATE. View state is a Base64-encoded string. In this example the amount of text stored in the view ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required