A.8. Chapter 8
A.8.1.
A.8.1.1.
A.8.1.1.1. Exercise 1 solution
A standard property uses a normal backing variable to store its value, whereas a ViewState property uses the ViewState collection for this. A normal property is reset on each postback, whereas a ViewState property is able to maintain its value. This advantage of the ViewState property comes at a cost, however. Storing the value in ViewState adds to the size of the page, both during the request and the response. A normal property doesn't have this disadvantage. You should carefully consider what you store in ViewState to minimize the page size.
A.8.1.1.2. Exercise 2 solution
To make the property maintain its state across postbacks, you need to turn it into a ViewState property. The required code is almost identical to that of the NavigateUrl, but it uses the Direction data type instead of a string. For the VB.NET example, just modify the highlighted Get and Set parts of the property. For the C# example, you need to remove the automatic property and replace it with the following code.
VB.NET
Public Property DisplayDirection() As Direction
Get
Dim _displayDirection As Object = ViewState("DisplayDirection")
If _displayDirection IsNot Nothing Then
Return CType(_displayDirection, Direction)
Else
Return Direction.Vertical ' Not found in ViewState; return a default value
End If
End Get
Set(ByVal Value As Direction)
ViewState("DisplayDirection") = Value
End Set
End Property
C#
public Direction DisplayDirection { get { ...