oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button


State Management

As mentioned previously, ASP.NET automatically takes care of state management. To understand what I mean by state management, consider the following example, shown in Figure 3.

Figure 3
Figure 3. A simple ASP.NET application

The example in Figure 3 prompts the user to enter a name and then click the Click Me button. The following code is the code behind for the application. When the button is clicked, it displays the name of the user in the Label control:

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click( _
       ByVal sender As Object, _
       ByVal e As System.EventArgs) _
       Handles Button1.Click
        Label1.Text = "Hello  " & TextBox1.Text
    End Sub
End Class

Here is how it looks like when run (see Figure 4).

Figure 4
Figure 4. Testing the application

Notice that after the button is clicked, the text contained within the TextBox control is still available. If you were using classic ASP, you would need to explicitly write code in your application to ensure that the text in the TextBox control remained visible.

To understand how ASP.NET maintains state between postbacks (when you submit a request back to the server), examine the source of the page in Internet Explorer (do a View -> Source):


<html xmlns="">
	Untitled Page
   <form method="post" action="Default.aspx" id="form1">
ZWktTWVuZyBMZWVkZGQVx6NLNcu0P+TBFnYu2ubB4x3vdQ==" />

        Please enter your name:<br />
        <input name="TextBox1" type="text" value="Wei-Meng Lee" 
id="TextBox1" />
        <input type="submit" name="Button1" value="Click Me" 
id="Button1" />
        <br />
        <span id="Label1">Hello  Wei-Meng Lee</span><br />
        <br />

In particular, look out for the hidden input element known as _VIEWSTATE. This element contains the state information of the page and is passed between the client and the server. By storing information in this element, the server is able to maintain the state of the page as it gets posted back to the server. (It does so by examining the content of the _VIEWSTATE element.)

Note that the information in the _VIEWSTATE element is only encoded, not encrypted.


Next Pagearrow