View State
In the preceding example 3 (DisplayMessage.aspx), it is seen that when you submit a form built from ASP.NET controls, the data entered into all the form fields is preserved at the time of submission. Microsoft calls this automatic persistence of data - View State. Therefore the View State is automatically preserved by ASP.NET control.
A simple HTML form offers no benefits of View State. With the standard HTML form, no difference is noticed between the first time the form is submitted and the seventh time You have to start with active form fields every time.
It is interesting to note that View State does not apply only to the ASP.NET controls that correspond to form elements; all the standard ASP.NET controls preserve their state.
For example, you can take a standard HTML <span> tag and convert it into an ASP.NET control like this:
| <span id=”myLabel” Runat=”Server”> |
In the line you magically convert the <span> tag into an ASP.NET control by adding an ID attribute and Runat=”Server” attribute. Now that the <span> tag is a server side control, any text assigned to it is preserved every time a Web Form Page that contains the tag is posted back to itself. This is illustrated in the Example 4.
| Example4:Guestbook.aspx |
<Script Runat="Server">
Sub Button_Click(s As Object, e As EventArgs )
entries.innerHtml = "<hr>" & username.Value & "<p>" & comments.Value & entries.innerHTML
End Sub
</Script>
<html>
<head><title>Guestbook.aspx</title></head>
<body>
<form Runat="Server">
<b>Username:</b>
<br><input id="username" type="text" size="30" Runat="Server">
<p>
<b>Comments:</b>
<br><textarea id="comments" cols=60 rows=10 Runat="Server"></textarea>
<p>
<input type="submit" value="Add Comment"
OnServerClick="Button_Click" Runat="Server">
<span id="entries" Runat="Server" />
</form>
</body>
</html> |
| |
In this example the text entered into the username and comments form fields, is preserved whenever the form is submitted. The View State of the username and comments control is automatically preserved, as shown below:
All the previous entries in the guest book are preserved at the bottom of the form. These entries are stored in the <span> tag’s view state. (The <span> tag with entries ID). Every time when the form is posted the contents of the username and comments field is added to the <span> tags View State.
The contents of the username and comment fields are added to the text displayed by the <span> tag by modifying the <span> tag’s innerHTML property. Every time a new Guest Book entry is added, the following code will be executed:
Entries.innerHTML=”<hr>” & username.Value & “<p>” & comments.Value & entries.innerHTML
However, you should know as how is the View State of the <span> tag preserved?
When you look at the source of the HTML page (in Microsoft Internet Explorer, select View Source, or in Netscape Navigator, select View, Page Source), a hidden form field will be seen that looks something like this:
| <input type="hidden" name="__VIEWSTATE" value="dDwyNTE2MTA0MzQ7dDw7bDxpPDE+Oz47bDx0PDtsPGk8Mz47a Tw3Pjs+O2w8dDxwPGw8aW5uZXJodG1sOz47bDxBU1AuTkVUIGlzIHZlcnkgc G93ZXJmdWxsOz4+Ozs+O3Q8cDxsPGlubmVyaHRtbDs+O2w8XDxoclw+ RGF2aWRcPHBcPkFTUC5ORVQgaXMgdmVyeSBwb3dlcmZ1bGxcPGhyXD5K b2huXDxwXD5BU1AuTkVUIGlzIEdvb2Q7Pj47Oz47Pj47Pj47Ph/jJKTgptRcqUVtA 1enqewhZ6hu" /> |
Well this hidden form field contains all the previous entries in the guest book.
The View State of all the controls contained in a Web Forms Page is preserved in the hidden form field named VIEWSTATE. View State is preserved only if a form is posted back to itself.
If you leave the Guestbook.aspx page and visit the Yahoo! Home page and return again, the view state of Guestbook.aspx is lost. Or, if a form is posted to another page, the view state information will be lost.
|