14.5. Using ViewState

ViewState is another ASP.NET feature that allows you to easily maintain the state of your controls between roundtrips to the server. As the page makes this trip to the server and back, it remembers the state of each control, including the controls to which you bind data to, and it populates each control's status back into the control as the page is redrawn. It does this by including a hidden form field element within your form page. If you look at the source of your .aspx page, notice that there is a ViewState model right at the beginning of the form. Listing 14-7 displays the beginning of an ASP.NET page.

Example 14-7. The top of the ASP.NET 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>
 Working with Cookies
</title></head>
<body>
    <form name="form1" method="post" action="Default6.aspx" id="form1">
<div>
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"
 value="/wEPDwUKLTg1OTgwNDQzOWRkMd6LzIcVw3I5z+i7jJ4zbUMFgQg=" />

This unreadable mess within the hidden form field shows the state of all the controls on the Web page. Instead of listing the state of the controls directly, this information is put into a format that is not readable by you and me, but is readable by the ...

Get Professional ASP.NET 3.5 AJAX 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.