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 ASP.NET parser. The parser takes this data and repopulates that page's controls.

14.5.1. Toggling ViewState on and Off

Keeping the ViewState functionality on is not always a priority with every Web page you create. For this reason, you can turn off the ViewState, thus saving server resources and increasing the speed of your application. You can turn off this functionality in two ways. The first is to disable ViewState on the page level, and the other is to disable it on the control level. To disable ViewState for the entire page, turn off this functionality within the page directive.

To turn off the ViewState functionality for the entire page, just add the following attribute to the page directive at the top of the page:

<%@ Page EnableViewState="False" %>

It is also possible to disable ViewState on the control level. If maintaining a control's state is not an important feature of that control, turn it off. This mildly increases the performance of the page overall. To turn off ViewState for a control, add the EnableViewState attribute to the control, use the following:

<asp:Label id="Label1" Runat="server" EnableViewState="False" />

Paying attention to which pages and controls are ViewState-enabled leads to better overall application performance.

14.5.2. Extending ViewState

There are times when you might want to include user-specific information in your Web pages that needs to be carried across server roundtrips but is beyond the state of the control. In this case, it is possible to piggyback onto the ViewState functionality. In a sense, you are adding your own set of name-value pairs to ViewState. This bit of code shows a simple example of using this functionality to incorporate a page-count function.

ViewState["count"] = 1;

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset