View State
As we saw briefly in the previous section, 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 use hacks to 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 theyre 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
ASP (not ASP.NET!) page, called Sample.asp:
File: Sample.asp
<!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>
Cassini and ASP
Cassini is an ASP.NET-only web server and will not execute pages written
in ASP, such as Sample.asp above. Fortunately, this wont be a problem
as you work your way through this book, as the above Sample.asp file is
the only ASP code in this book.
44
Chapter 2: ASP.NET Basics
If you save this as Sample.asp in the Learning virtual directory you created in
Chapter 1, and open it in your browser by entering http://localhost/Learn-
ing/Sample.asp, youll see that view state is not automatically preserved. When
the user submits the form, the information that was typed into the text box is
cleared, although its still available in Request.Form("nameTextBox"). The
equivalent page in ASP.NET, ViewState.aspx, demonstrates this data persistence
using view state:
Visual Basic File: ViewState.aspx
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>View State Example</title>
<script runat="server" language="VB">
Sub Click(s As Object, e As EventArgs)
messageLabel.Text = nameTextBox.Text
End Sub
</script>
</head>
<body>
<form runat="server">
<asp:TextBox id="nameTextBox" runat="server" />
<asp:Button id="submitButton" runat="server"
Text="Click Me" OnClick="Click" />
<asp:Label id="messageLabel" runat="server" />
</form>
</body>
</html>
C# File: ViewState.aspx
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>View State Example</title>
<script runat="server" language="C#">
void Click(Object s, EventArgs e)
{
messageLabel.Text = nameTextBox.Text;
}
</script>
</head>
<body>
<form runat="server">
45
View State
<asp:TextBox id="nameTextBox" runat="server" />
<asp:Button id="submitButton" runat="server"
Text="Click Me" OnClick="Click" />
<asp:Label id="messageLabel" runat="server" />
</form>
</body>
</html>
In this case, the code uses ASP.NET controls with the runat="server" attribute.
As you can see in Figure 2.4, 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 2.4. ASP.NET maintaining the state of the controls
You can see the benefits of view state already. But wheres 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 youve submitted the form,
and look for the following code:
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"
value="/wEPDwUKLTEwNDY1Nzg0MQ90fMCR+FN5P6v5pkTQwNEl5xhBk" />
This is a standard HTML hidden form field. All information thats 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 do not 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" %>
46
Chapter 2: ASP.NET Basics

Get Build Your Own ASP.NET 2.0 Web Site Using C# & VB, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.