WindowsDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


AddThis Social Bookmark Button

What Is ASP.NET
Pages: 1, 2, 3, 4, 5, 6, 7, 8

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):


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
	Untitled Page
</title></head>
<body>
   <form method="post" action="Default.aspx" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" 
value="/wEPDwUKMTU5MTA2ODYwOQ9kFgICAw9kFgICBQ8PFgIeBFRleHQFE0hlbGxvICBX
ZWktTWVuZyBMZWVkZGQVx6NLNcu0P+TBFnYu2ubB4x3vdQ==" />
</div>

    <div>
        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 />
    
    </div>
    </form>
</body>
</html>

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.

Pages: 1, 2, 3, 4, 5, 6, 7, 8

Next Pagearrow