Skip to Main Content
ASP.NET 4 24-Hour Trainer
book

ASP.NET 4 24-Hour Trainer

by Toi B. Wright
July 2010
Beginner content levelBeginner
552 pages
10h 14m
English
Wrox
Content preview from ASP.NET 4 24-Hour Trainer

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

ASP.NET 4 24-Hour Trainer

ASP.NET 4 24-Hour Trainer

Toi B. Wright
ASP.NET 4 Unleashed

ASP.NET 4 Unleashed

Stephen Walther, Kevin Hoffman, Nate Dudek

Publisher Resources

ISBN: 9780470596913Purchase bookExamplesErrata